1

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)

H H
  • 263,252
  • 30
  • 330
  • 514
Dmitriy Sosunov
  • 1,075
  • 3
  • 10
  • 25
  • Are you telling me that no one has answered 6 of your 9 questions? – Shawn Mclean Feb 06 '11 at 20:13
  • I need to know what's wrong in this case. And why include doesn't work. – Dmitriy Sosunov Feb 06 '11 at 20:31
  • You're not giving us much to go on... what do you expect this to do, what is it doing, what does your data model look like, ... – Yannick Motton Feb 06 '11 at 20:36
  • Yannick, I expect that the Comment entity will have CommentType, Owner and Message properties will be loaded. This query returns the correct set of comments, but they have empty properties. And I want to push EF to load these properties. – Dmitriy Sosunov Feb 06 '11 at 20:44

1 Answers1

1

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();
Yannick Motton
  • 34,761
  • 4
  • 39
  • 55
  • Yannick, I have read post on MSDN, but I have still don't understand why in the following query include() not work: var messages = ctx.Messages.Include("Account"); var previews = ctx.MessagePreviews; var query = from m in messages join p in previews on m.IDALIAS equals p.IDALIAS where m.Id == messageId select new Pair { First = m, Second = p }; – Dmitriy Sosunov Feb 07 '11 at 05:30
  • As far as I understood, it has to do with the way Includes are applied. Apparently when the resulttype of the query changes by joining(`SelectMany`), EF will not apply the includes. – Yannick Motton Feb 07 '11 at 20:15