I\'m doing a lambda select on EF4.1, including another related DBSet in my current statement.
return dbEntity.GameTypes.Include(a => a.Draws)
From MSDN for DbExtensions.Include().
The path expression must be composed of simple property access expressions together with calls to Select in order to compose additional includes after including a collection property.
So I don't think using Where()
is allowed. I'm not sure if you can do any filtering when it comes to Include()
.
I think....
from g in dbEntity.GameTypes.Include("Draws")
where g.IsActive
let d = g.Draws.Where(o => o.DrawDate > System.DateTime.Now)
.OrderBy(o => o.DrawDate)
.Take(1) // Needs to stay a collection
select new GameType {IsActive = g.IsActive, Draws = d}
untested - but it might get you on the right path...
I've never been successful at finding a way to filter the children like you want to do. In order to reduce the volume of data being retrieved from the database I will usually retrieve just the parent objects and then loop over them getting only the children I want and "attach" them to the parent. That's not a hard-and-fast rule though, it kind of depends on how many children there are for each parent and what percentage of them I'm likely to want to keep.