This question already has answers here:
EF Core Second level ThenInclude missworks

(2 个回答)


2年前关闭。




对于像下面这样的模型
public class Parent
{
    public int Id { get; set; }

    public List<Child> Children { get; set; }
}

public class Child
{
    public int ChildId { get; set; }

    public int ParentId { get; set; }

    public List<Child> GrandChildren { get; set; }
}

public class GrandChild
{
    public int GrandChildId { get; set; }

    public int ChildId { get; set; }
}

如何编写一个查询来获取 Include/ThenInclude 语句中的所有 Children 和 GrandChildren?
var record = GetAll()
 .Where(r => r.Id == 4)
 .Include(r => r.Children)
     .ThenInclude(????????????)

在“ThenInclude”语句中,我不能使用 select 语句来获取孙子。获取 GrandChildren 收藏的正确方法是什么?

非常感激。

最佳答案

只需输入导航属性名称:

.ThenInclude(c => c.GrandChildren)

这是 EF Core 文档的 Including multiple levels 部分中特别提到的当前 Intellisense 问题:

关于c# - EFCore 2.0 - Include & ThenInclude : child collection -- grandchild collection,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51547369/

10-11 13:52