首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从根父级检索GrandChild集合

从根父级检索GrandChild集合
EN

Stack Overflow用户
提问于 2014-06-23 15:07:06
回答 2查看 729关注 0票数 0

我有以下对象图:

根( Root_Id)

-儿童(Child_Id,Root_Id)

-孙女(GrandChild_Id,Child_Id)

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

代码语言:javascript
复制
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)参数实例:无法将“AnonymousType#1[]”转换为“System.Linq.IQueryable”

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-23 15:16:10

尝尝这个

代码语言:javascript
复制
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;

我仍然对你的目标有些不确定,但它看起来像是你原来的解决方案的工作近似。

编辑:

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

代码语言:javascript
复制
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;}
}

然后,正如AD.NET建议的那样,您可以尝试SelectMany方法。

代码语言:javascript
复制
        GrandChild gc1 = new GrandChild(1, 10);
        GrandChild gc2 = new GrandChild(2, 10);
        GrandChild gc3 = new GrandChild(3, 11);

        Child c1 = new Child(10, 100, new GrandChild[]{ gc1, gc2 });
        Child c2 = new Child(11, 100, new GrandChild[]{ gc3 });

        Root r1 = new Root(new Child[]{c1, c2});

        Db db = new Db(new Root[] { r1 });

        var rootGrandChildren = db
            .Roots
            .FirstOrDefault()
            .Children
            .SelectMany(child => child.GrandChildren);

在查询语法中,它将类似于

代码语言:javascript
复制
    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;

但是如果您的Child类不知道他的GrandChildren,那么它们(GrandChildren)包含在根中:

代码语言:javascript
复制
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; }
}

您必须使用:

代码语言:javascript
复制
  Root r1 = new Root(new Child[]{c1, c2}, new GrandChild[]{gc1, gc2, gc3});

  Db db = new Db(new Root[] { r1 });


  Root root = db.Roots.FirstOrDefault();

  var rootGrandChildren = from child in root.Children
                              join grandChild in root.GrandChildren
                              on  child.Child_Id equals grandChild.Child_Id
                          select grandChild;
票数 1
EN

Stack Overflow用户

发布于 2014-06-23 15:26:43

代码语言:javascript
复制
             db.Root
            .SingleOrDefault( r => r.Root_Id == rootID )
            .Childs.SelectMany(ch=>ch.GrandChilds).Distinct()

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24369291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档