2

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

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

7 Comments

Z is an array, so I am unable to write Z.W :(
Then what was Include("Z.W") meant to do in your original example?
Z in an Object, but X has an array of Z.
Then Include("Z.W") wouldn't have worked as-intended, or it would load a large amount of data from your database inefficiently. BTW you should use the Include(lambda) version instead of the Include(String) method, as it offers compile-time safety verification.
Good idea, but what about avoiding the null? It still null.. Include(a => X.Z) is giving me IQueryable Object, so I can not go deeper on the class to access W.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.