C# 从根父级检索孙子集合

C# 从根父级检索孙子集合,c#,linq,entity-framework,linq-to-entities,C#,Linq,Entity Framework,Linq To Entities,我有以下对象图: 根目录(根目录Id) ----子(子Id,根Id) -------孙辈(孙辈Id、子女Id) 我想绕过Child并返回具有根对象的孙子集合。到目前为止,我已经尝试过: var child_Ids = db.Root .SingleOrDefault( r => r.Root_Id == rootID ) .Childs .Select( ch => new { Chil

我有以下对象图:

根目录(根目录Id)
----子(子Id,根Id)
-------孙辈(孙辈Id、子女Id)

我想绕过Child并返回具有根对象的孙子集合。到目前为止,我已经尝试过:

var child_Ids = db.Root
                .SingleOrDefault( r => r.Root_Id == rootID )
                .Childs
                .Select( ch => new {  Child_Id = ch.Child_Id} ).ToArray();

 return db.GrandChilds.Where( gc => child_Ids.Contains( gc.Child_Id ) );
但它甚至不会编译并出现以下错误:
1) IEnumerable不包含包含的定义
2) 参数实例:无法从“匿名类型#1[]”转换为“System.Linq.IQueryable” 我怎样才能做到这一点呢?

试试这个

var child_Ids = db.Root
    .SingleOrDefault( r => r.Root_Id == rootID )
    .Childs
    .Select( ch => ch.Child_Id)
    .ToArray();

 return 
     from grandChild in db.GrandChild
         join child_id in child_Ids
         on child_id == grandChild.HandlingUnit_Id 
     select grandChild;
附言:我仍然有点不确定你的目标,但它看起来像是你原来的解决方案的近似工作

编辑:

如果您的层次结构和类类似于:

public class Db
{
    public Db(IEnumerable<Root> roots)
        {  this.Roots = new List<Root>(roots); }

    public ICollection<Root> Roots { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children )
    { 
        this.Children = new List<Child>(children);
    }

    public ICollection<Child> Children { get; private set; }
}


public class Child
{
    public Child(Int32 childId, Int32 rootId, IEnumerable<GrandChild> grandChildren)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
        this.GrandChildren = new List<GrandChild>(grandChildren);
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
    public ICollection<GrandChild> GrandChildren {get; private set;}
}

public class GrandChild
{
    public GrandChild (Int32 grandChildId, Int32 childId)
    { 
        this.GrandChild_Id = grandChildId; 
        this.Child_Id = childId; 
    }

    public Int32 GrandChild_Id {get; private set;}
    public Int32 Child_Id {get; private set;}
}
在查询语法中,它看起来像

    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;
但是如果您的
子类
不知道他的孙子,并且他们(孙子)包含在根目录中:

public class Child
{
    public Child(Int32 childId, Int32 rootId)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children, IEnumerable<GrandChild> grandChildren )
    { 
        this.Children = new List<Child>(children);
        this.GrandChildren = new List<GrandChild>(grandChildren );
    }

    public ICollection<Child> Children { get; private set; }
    public ICollection<GrandChild> GrandChildren{ get; private set; }
}
试试这个

var child_Ids = db.Root
    .SingleOrDefault( r => r.Root_Id == rootID )
    .Childs
    .Select( ch => ch.Child_Id)
    .ToArray();

 return 
     from grandChild in db.GrandChild
         join child_id in child_Ids
         on child_id == grandChild.HandlingUnit_Id 
     select grandChild;
附言:我仍然有点不确定你的目标,但它看起来像是你原来的解决方案的近似工作

编辑:

如果您的层次结构和类类似于:

public class Db
{
    public Db(IEnumerable<Root> roots)
        {  this.Roots = new List<Root>(roots); }

    public ICollection<Root> Roots { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children )
    { 
        this.Children = new List<Child>(children);
    }

    public ICollection<Child> Children { get; private set; }
}


public class Child
{
    public Child(Int32 childId, Int32 rootId, IEnumerable<GrandChild> grandChildren)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
        this.GrandChildren = new List<GrandChild>(grandChildren);
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
    public ICollection<GrandChild> GrandChildren {get; private set;}
}

public class GrandChild
{
    public GrandChild (Int32 grandChildId, Int32 childId)
    { 
        this.GrandChild_Id = grandChildId; 
        this.Child_Id = childId; 
    }

    public Int32 GrandChild_Id {get; private set;}
    public Int32 Child_Id {get; private set;}
}
在查询语法中,它看起来像

    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;
但是如果您的
子类
不知道他的孙子,并且他们(孙子)包含在根目录中:

public class Child
{
    public Child(Int32 childId, Int32 rootId)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children, IEnumerable<GrandChild> grandChildren )
    { 
        this.Children = new List<Child>(children);
        this.GrandChildren = new List<GrandChild>(grandChildren );
    }

    public ICollection<Child> Children { get; private set; }
    public ICollection<GrandChild> GrandChildren{ get; private set; }
}
试试这个

var child_Ids = db.Root
    .SingleOrDefault( r => r.Root_Id == rootID )
    .Childs
    .Select( ch => ch.Child_Id)
    .ToArray();

 return 
     from grandChild in db.GrandChild
         join child_id in child_Ids
         on child_id == grandChild.HandlingUnit_Id 
     select grandChild;
附言:我仍然有点不确定你的目标,但它看起来像是你原来的解决方案的近似工作

编辑:

如果您的层次结构和类类似于:

public class Db
{
    public Db(IEnumerable<Root> roots)
        {  this.Roots = new List<Root>(roots); }

    public ICollection<Root> Roots { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children )
    { 
        this.Children = new List<Child>(children);
    }

    public ICollection<Child> Children { get; private set; }
}


public class Child
{
    public Child(Int32 childId, Int32 rootId, IEnumerable<GrandChild> grandChildren)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
        this.GrandChildren = new List<GrandChild>(grandChildren);
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
    public ICollection<GrandChild> GrandChildren {get; private set;}
}

public class GrandChild
{
    public GrandChild (Int32 grandChildId, Int32 childId)
    { 
        this.GrandChild_Id = grandChildId; 
        this.Child_Id = childId; 
    }

    public Int32 GrandChild_Id {get; private set;}
    public Int32 Child_Id {get; private set;}
}
在查询语法中,它看起来像

    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;
但是如果您的
子类
不知道他的孙子,并且他们(孙子)包含在根目录中:

public class Child
{
    public Child(Int32 childId, Int32 rootId)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children, IEnumerable<GrandChild> grandChildren )
    { 
        this.Children = new List<Child>(children);
        this.GrandChildren = new List<GrandChild>(grandChildren );
    }

    public ICollection<Child> Children { get; private set; }
    public ICollection<GrandChild> GrandChildren{ get; private set; }
}
试试这个

var child_Ids = db.Root
    .SingleOrDefault( r => r.Root_Id == rootID )
    .Childs
    .Select( ch => ch.Child_Id)
    .ToArray();

 return 
     from grandChild in db.GrandChild
         join child_id in child_Ids
         on child_id == grandChild.HandlingUnit_Id 
     select grandChild;
附言:我仍然有点不确定你的目标,但它看起来像是你原来的解决方案的近似工作

编辑:

如果您的层次结构和类类似于:

public class Db
{
    public Db(IEnumerable<Root> roots)
        {  this.Roots = new List<Root>(roots); }

    public ICollection<Root> Roots { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children )
    { 
        this.Children = new List<Child>(children);
    }

    public ICollection<Child> Children { get; private set; }
}


public class Child
{
    public Child(Int32 childId, Int32 rootId, IEnumerable<GrandChild> grandChildren)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
        this.GrandChildren = new List<GrandChild>(grandChildren);
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
    public ICollection<GrandChild> GrandChildren {get; private set;}
}

public class GrandChild
{
    public GrandChild (Int32 grandChildId, Int32 childId)
    { 
        this.GrandChild_Id = grandChildId; 
        this.Child_Id = childId; 
    }

    public Int32 GrandChild_Id {get; private set;}
    public Int32 Child_Id {get; private set;}
}
在查询语法中,它看起来像

    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;
但是如果您的
子类
不知道他的孙子,并且他们(孙子)包含在根目录中:

public class Child
{
    public Child(Int32 childId, Int32 rootId)
    {
        this.Child_Id = childId;
        this.Root_Id = rootId; 
    }

    public Int32 Child_Id { get; private set; }
    public Int32 Root_Id { get; private set; }
}

public class Root
{
    public Root(IEnumerable<Child> children, IEnumerable<GrandChild> grandChildren )
    { 
        this.Children = new List<Child>(children);
        this.GrandChildren = new List<GrandChild>(grandChildren );
    }

    public ICollection<Child> Children { get; private set; }
    public ICollection<GrandChild> GrandChildren{ get; private set; }
}
使用
.SelectMany
扩展名获取孙辈收藏

使用
.SelectMany
扩展名获取孙辈收藏

使用
.SelectMany
扩展名获取孙辈收藏


使用
.SelectMany
扩展获取孙子集合

目标是获取具有根父对象的孙子集合,我的用例不需要子集合。我会试试你的建议,让你知道。但是你的原始示例中的gc.HandlingUnit_Id是什么?对不起,一定是打字错误。(返回db.grangers.Where(gc=>child_Id.Contains(gc.child_Id));)。我更新了这个问题,以反映目标是获得具有根父对象的孙子集合,我的用例不需要子集合。我会试试你的建议,让你知道。但是你的原始示例中的gc.HandlingUnit_Id是什么?对不起,一定是打字错误。(返回db.grangers.Where(gc=>child_Id.Contains(gc.child_Id));)。我更新了这个问题,以反映目标是获得具有根父对象的孙子集合,我的用例不需要子集合。我会试试你的建议,让你知道。但是你的原始示例中的gc.HandlingUnit_Id是什么?对不起,一定是打字错误。(返回db.grangers.Where(gc=>child_Id.Contains(gc.child_Id));)。我更新了这个问题,以反映目标是获得具有根父对象的孙子集合,我的用例不需要子集合。我会试试你的建议,让你知道。但是你的原始示例中的gc.HandlingUnit_Id是什么?对不起,一定是打字错误。(返回db.grangers.Where(gc=>child_Id.Contains(gc.child_Id));)。我更新了问题以反映编译错误是什么?属性名称可能不同编译错误是什么?属性名称可能不同编译错误是什么?属性名称可能不同编译错误是什么?属性名称可能不同