1

I have nested entites like example from msdn.

var blogs1 = context.Blogs 
                   .Include(b => b.Posts.Select(p => p.Comments)) 
                   .ToList(); 

But in my case, Comment have structure like this:

public class Comment
{
    public List<User> ViewedUsers { get; set; };
}

How can i load ViewedUsers in this case, if method .ThenInclude() is unavalable? I mean new layer of nested entites.

flybox
  • 155
  • 1
  • 9
  • you mean like `.Include(b => b.Posts.Select(p => p.Comments.Select(c => c.ViewedUsers)))`? – grek40 Nov 17 '16 at 09:50
  • Possible duplicate of [Entity framework linq query Include() multiple children entities](http://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities) – ste-fu Nov 17 '16 at 09:56
  • @grek40 Yes, like this. But this example dont work. – flybox Nov 17 '16 at 09:56
  • @ste-fu Not a duplicate, add additional layer of nested entites. If employee have new nested entity of Employee_Car in your example. – flybox Nov 17 '16 at 09:58
  • @flybox I just tried exactly as I commented and it is working for me. Be sure to test your code precisely the way you ask, because different things can screw the include (like `Include(...).Select(...)` will not work as expected). – grek40 Nov 17 '16 at 10:16
  • @grek40 Use .Select not on p.Comments, use it after bracket like in my answer. It works. – flybox Nov 17 '16 at 11:00
  • 1
    @flybox wait a second... is `Post.Comments` not a collection? – grek40 Nov 17 '16 at 11:06
  • @grek40 Collection, Comment is element of collection Comments. – flybox Nov 17 '16 at 11:10
  • @grek40 i was wrong, testing this on not a collection element. You are right. – flybox Nov 17 '16 at 11:12
  • @grek40 i changed answer and understood, what is wrong in my case. thanks! – flybox Nov 17 '16 at 11:13

1 Answers1

0

In this case i need to use next statement:

var blogs1 = context.Blogs 
               .Include(b => b.Posts.Select(p => p.Comments.Select(cm => cm.ViewedUsers))) 
               .ToList(); 
flybox
  • 155
  • 1
  • 9