问题
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!
回答1:
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();
来源:https://stackoverflow.com/questions/48096194/ef-linq-include-nested-entities