LINQ To Entities + Include + Anonymous type issue

As Ladislav mentioned, Include only works if you select the Ticket entity directly. Since you’re projecting other information out, the Include gets ignored.

This should provide a good work-around:

var data = ctx.Set<Ticket>()
    .Select(p => new 
         { 
             Ticket = p, 
             Clients = p.Client,
             LastReplyDate = p.Replies.Max(q => q.DateCreated)
         });

First of all, each Ticket’s Clients will be accessible directly from the Clients property on the anonymous type. Furthermore, Entity Framework should be smart enough to recognize that you have pulled out the entire Client collection for each Ticket, so calling .Ticket.Client should work as well.

Leave a Comment