sa94
sa94

Reputation: 29

Entity Framework 6 - Null value in nested query with select

I am facing a problem in EF6. When I execute the query Select it return the value. But when I add Select it returns null.

The code is here:

The (W) is not null here...

        var list = db.X.Include("Y").Include("Z.W")
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

Here, The W value is null...

        var list = db.X.Include("Y").Include("Z.W")
            .Select(a => new { a.Id, a.Z})
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

Please help :)

UPDATE 1

public class academy
{
    public int Id { get; set; }
   [StringLength(255)]
    [Index(IsUnique = true)]
    public string Name { get; set; }
    public string Logo { get; set; }
    [Required]
    public  Owner owner { get; set; }

    public  List<location> Location { get; set; }           
}


public class location
{
    public int Id { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string Neighborhood { get; set; }
    public string Street { get; set; }

    public  academy Academy { get; set; }

    public  List<stadium> Stadiums { get; set; }

    public List<Administrators> Administrators { get; set; }

    public List<addition> Addition { get; set; }

    public List<Pricing> Pricing { get; set; }

    public List<time_frame> TimeFrames { get; set; }

    [NotMapped]
    public string Details {
        get { return (City + " - " + Street); }
    }

}


public class Pricing
{

    public int Id { get; set; }
    public double Price { get; set; }
    public double? PriceAfterOffer { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public location Location { get; set; }
    public players_capacity StadiumCapacity { get; set; }


}

public class players_capacity
{
    public int Id { get; set; }
    [StringLength(255)]
    [Index(IsUnique = true)]
    public string Capacity { get; set; }

}


        var list = db.locations
            .Select(a => new { a.Id, a.City, a.Region, a.Street, a.Latitude, a.Longitude, a.Pricing, a.Academy })
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

The problem is on players_capacity always null

Upvotes: 2

Views: 249

Answers (1)

Dai
Dai

Reputation: 155390

Any additional data specified by Include is ignored if the query changes "shape", in this case your additional .Select expression invalidates the previous Include terms so they are ignored. The same happens if you do a GroupBy or GroupJoin.

Fortunately the fix is simple: explicitly specify the Y and Z.W members in your projection:

var list = db.X
    .Select( x => new { x.Id, x.Z, x.Y, x.Z.W } )
    .OrderBy( p => p.Id )
    .Skip( () => pageSize * page )
    .Take( () => pageSize )
    .ToList();

(Note that I'm using the Expression<> overloads of Skip and Take, as those are better for use with EF).

Upvotes: 2

Related Questions