Serg046
Serg046

Reputation: 1173

Include() does not work

var x = connection.Set<Team>()
    .Include(t => t.Level)
    .Select(t => 
        new {Team = t, LevelForTesting = t.Level})
    .ToList()

Why I don't get the object x[0].Team.Level (I have 'null') but get the object x[0].LevelForTesting? How can I change this code to get x[0].Team.Level? Thanks.

Upvotes: 0

Views: 71

Answers (1)

Alexander Derck
Alexander Derck

Reputation: 14498

You are throwing away the results of your eager loading by the anonymous select. Just drop the select and you will be able to access the Level in your list of Team:

var x = connection.Set<Team>().Include(t => t.Level).ToList();
var level = x[0].Level;

To get a better understanding of lazy/eager loading you should read this. Basically eager loading populates the specified navigation properties of your list of entities.

Upvotes: 2

Related Questions