获取任何子(或孙子)满足条件的所有根元素

时间:2016-03-02 10:06:09

标签: c# entity-framework linq

我的SQL数据库中有一个自引用的层次结构表,我使用EntityFramework数据库优先导入。这是我从中得到的课程:

public partial class Dimension
{
    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public Nullable<System.Guid> ParentDimensionId { get; set; }

    public virtual ICollection<Dimension> ChildDimensions { get; set; }
    public virtual Dimension ParentDimensions { get; set; }
}

课程和参考资料完成工作。但是现在我想编写一个LINQ查询,它允许我获得所有根维度(例如,其中ParentDimensionId == null),其中有任何子项符合某个条件。

我的问题是,在这个层次结构中可能有 n个级别,我必须为每个孩子,孙子等检查这个条件。

所以我真的需要一些像这样的扩展方法:

List<Dimension> dimensions = db.Dimension.Where(x =>
   x.ParentDimension == null && // This is the "root" identifier
   x.ChildDimensions.AnyChildWhere( c => c.Name == "foo") // This is the condition
).ToList()

这&#34; AnyChildWhere&#34;方法当然需要能够通过整个n级层次结构树,直到没有孩子离开。

有什么想法吗?

0 个答案:

没有答案
相关问题