Well, first of all, if you want to make sure that everything is loading during the execution of the request, you should add an explicit Include
:
context. Games. Include(g => g.Series.Teams.Select(t => t.Players)). Where(g => g.StartTime >= DateTime.Now && g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))). ToList();
However, as I mentioned in my comment, this will not produce the same results as your SQL query, since you are not filtering out players from the child collection.
In EF 4.1, there are some excellent Use of filters when explicitly loading related objects , but I could not get it to work for sub-sub-collections, so I think that the closest to the original query may be projecting the results onto an anonymous object (or you can create a class for it, if you need to pass this object later):
var query = context. Games. Where(g => g.StartTime >= DateTime.Now && g.Series.Teams.Any(t => t.Players.Any(p => p.Id == 1))). Select(g => new { Game = g, Players = g. Series. Teams. SelectMany(t => t. Players. Where(p => p.Id == user.Id)) });
Then you can list and check the results:
var gamesAndPlayersList = query.ToList();
Yakimych
source share