Consider:
Class Client
Class Project
Class Ticket
Class Reply
Clients have a sub collection of projects, projects have a sub collection of tickets and tickets have a sub collection of replies.
var data = ctx.Set<Ticket>().Include(p => p.Client).
Select(p => new { Ticket = p, LastReplyDate = p.Replies.Max(q => q.DateCreated)});
Doesn’t work. Neither project nor client are loaded when selecting data this way.
I know how to make it work. My question is why doesn’t it work like this?
As Ladislav mentioned,
Includeonly works if you select theTicketentity directly. Since you’re projecting other information out, theIncludegets ignored.This should provide a good work-around:
First of all, each Ticket’s Clients will be accessible directly from the
Clientsproperty on the anonymous type. Furthermore, Entity Framework should be smart enough to recognize that you have pulled out the entireClientcollection for eachTicket, so calling.Ticket.Clientshould work as well.