EF Core 1.0:如何查詢整棵樹?

c# entity-framework-core linq tree

我正在使用EF 7.0.0.0-rc1-final。

我有一個樹結構,從GrandGrandParent到GrandParent到Parent到Child的一對多關係:

public class GrandGrandParent
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual List<GrandParent> GrandParents { get; set; }

    public GrandGrandParent()
    {
        this.GrandParents = new List<GrandParent>();
    }
}

public class GrandParent
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual GrandGrandParent GrandGrandParent { get; set; }
    public virtual List<Parent> Parents { get; set; }

    public GrandParent()
    {
        this.Parents = new List<Parent>();
    }
}

public class Parent
{
    public int ID { get; set; }
    public string Name { get; set; }

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

    public Parent()
    {
        this.Children = new List<Child>();
    }
}

public class Child
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual Parent Parent { get; set; }
}

使用EF Core 1.0(EF 7),如果給定一個特定的祖父 - 父ID,如何創建一個LINQ查詢(或子查詢)給我整個樹?

我可以。包括()向上或向下一級,也許我對顯而易見的人視而不見?這給了我GrandGrandParent和GrandParents列表:

var ggparent1 = from ggp in myDbContext.GrandGrandParent
                .Include(ggp => ggp.GrandParents)
                where ggp.ID == 2
                select ggp;

我想得到整棵樹,直到孩子的名單。我必須使用foreach()循環代碼並手動構建樹嗎?

一般承認的答案

我會選擇Linq表格:

private static void Test0(ApplicationDbContext myDbContext)
{
    var ggparent = myDbContext.GrandGrandParents
        .Include(ggp => ggp.GrandParents)
        .ThenInclude(gp => gp.Parents)
        .ThenInclude(p => p.Children)
        .FirstOrDefault(ggp => ggp.ID == 3);

    if (ggparent == null)
    {
        DebugPrint("GrandGrandParent not found");
        return;
    }

    DebugPrint("GrandGrandParent:");
    DebugPrint(ggparent);


    if (ggparent.GrandParents == null)
    {
        DebugPrint("GrandParents null");
        return;
    }

    foreach (var gparent in ggparent.GrandParents)
    {
        DebugPrint(gparent);
        if (gparent.Parents == null) continue;
        foreach (var parent in gparent.Parents)
        {
            DebugPrint(parent);
            if (parent.Children == null) continue;
            foreach (var child in parent.Children)
            {
                DebugPrint(child);
            }
        }
    }

    int changeCount = myDbContext.SaveChanges();
    DebugPrint(string.Format("ChangeCount={0}", changeCount));
}

然後,您不必自己進行子查詢。但是您可以添加日誌記錄以查看EF實際創建的SQL查詢。

如果您需要完整的樹,則不需要select 。您可以使用select來提取結果樹的一部分,或者從層次結構中構建一個新的(平面)對象。

我將僅使用兩個級別(具有多對多)的層次結構展平以獲取分組SelectList的項目的示例:

var result =
    (from c in dbContext.EventTypes
    join j in dbContext.EventType2EventTypes on c.Id equals j.ChildEventTypeId
    join p in dbContext.EventTypes on j.ParentEventTypeId equals p.Id
    where p.EventTypeLevel == EventTypeLevel.First && c.EventTypeLevel == EventTypeLevel.Second
    orderby p.SortOrder, p.Id, c.SortOrder, c.Id
    select new SelectListItem
    {
        Text = c.NameDe,
        Value = c.Id.ToString(),
        Group = GetParentEventTypeSelectListGroup(p.NameDe)
    }).AsNoTracking();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因