EF LINQ包含嵌套实体 [英] EF LINQ include nested entities

查看:57
本文介绍了EF LINQ包含嵌套实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有具有以下层次结构的多层次实体:父级-> RootChildren->子级->子级-> .....

I have multi-leveled entities with the following hierarchy: Parent-> RootChildren -> Children -> Children -> .....

Class Parent 
{
   public int Id {get; set;}
   public virtual Child RootChildren {get; set;}
}    
Class Child
{
   public virtual List<Child> Children {get; set;}
}

现在,我想以嵌套方式包括包含所有子项的实体.

Now, I want to include entity which include all the child as nested way.

我尝试了以下操作,但是没有用:

I tried the following but it didn't work:

var parents = dbContext.Parent 
                    .Where(p => p.Id == id)
                    .Select(r => r.RootChildren)
                    .Include(c => c.Children)
                    .ToList();

它为我的第一个孩子提供了结果,但不包括树中存在的所有嵌套孩子.

It gives me result for first children but does not include all the nested children present in the tree.

任何建议或信息将不胜感激.谢谢!

Any piece of advise or information would be highly appreciated. Thanks!

推荐答案

您不应尝试在 Select 之后执行 Include 并执行 Include 分层;

You shouldn't try to apply Include after Select and perform Include hierarchical;

var parents = dbContext.Parent
    .Where(p => p.Id == id)
    .Include(c => c.RootChildren.Children)
    .Select(r => r.RootChildren)
    .ToList();

这篇关于EF LINQ包含嵌套实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆