从根父级检索GrandChild集合

时间:2014-06-23 15:07:06

标签: c# linq entity-framework linq-to-entities

我有以下对象图:

Root(Root_Id)
----孩子(Child_Id,Root_Id)
------- GrandChild(GrandChild_Id,Child_Id)

我想绕过Child并返回具有Root对象的GrandChild集合。到目前为止,我已经尝试过这个:

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不包含Contains ...的定义 2)参数实例:无法转换为#AnonymousType#1 []'到System.Linq.IQueryable

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

             db.Root
            .SingleOrDefault( r => r.Root_Id == rootID )
            .Childs.SelectMany(ch=>ch.GrandChilds).Distinct()

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

答案 1 :(得分:1)

试试这个

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;
P.S:我对你的目标仍然有点不确定,但看起来像你的原始解决方案的近似工作

修改

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

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方法

        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);

在查询语法中,它看起来像

    var rootGrandChildren = from child in db.Roots.FirstOrDefault().Children
                            from grandChild in child.GrandChildren
                            select grandChild;

如果您的Child班级不了解他的GrandChildren且他们(GrandChildren)包含在Root中:

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; }
}

你将不得不使用:

  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;