Include Grandchildren in EF Query

前端 未结 2 531
死守一世寂寞
死守一世寂寞 2020-12-03 21:05

Given the object hierarchy

public class Parent
{
    public int Id { get; set; }
    public virtual Child Child { get; set; }
}

public class Child
{
    pub         


        
相关标签:
2条回答
  • 2020-12-03 21:10

    Update: If you are using Entity Framework Core you should use the following syntax

    var hierarchy = from p in ctx.Parents
                        .Include(p => p.Children)
                        .ThenInclude(c => c.GrandChild)
                    select p;
    
    0 讨论(0)
  • 2020-12-03 21:30

    Sure, you can do

    var hierarchy = from p in ctx.Parents
                        .Include(p => p.Children.Select(c => c.GrandChild))
                    select p;
    

    See MSDN, caption Remarks, the fifth bullet.

    0 讨论(0)
提交回复
热议问题