Dmitriy Sosunov
Dmitriy Sosunov

Reputation: 1113

Entity Framework & Include

All Please help me make include() work in the following case:

ctx.Messages
  .Include("Comments.CommentType")
  .Include("Comments.Owner")
  .Include("Comments.Message")
  .Where(m => m.RID == messageId)
  .SelectMany(m => m.Comments)
  .Where(c => c.CommentType.Id == commentTypeId)
  .ToList();

How I should rewrite this query?

P.S. I'm using EF 3.5 (not 4.0)

Upvotes: 1

Views: 1626

Answers (1)

Yannick Motton
Yannick Motton

Reputation: 35971

This is most likely related to an issue with Include and joins. Basically it comes down to this: Includes are only applied at the end of the statement, and due to joining, the type of your query changes from an IQueryable<Message> to an IQueryable<Comment>.

By removing the join, it should correctly include the navigation properties. Try the following:

ctx.Comments
   .Include("CommentType")
   .Include("Owner")
   .Include("Message")
   .Where(c => c.Message.RID == messageId && c => c.CommentType.Id == commentTypeId)
   .ToList();

Upvotes: 1

Related Questions