Linq查询以基于孙辈的属性返回祖辈、父母和孙辈

Linq查询以基于孙辈的属性返回祖辈、父母和孙辈,linq,Linq,为这个笨拙的标题道歉。我需要查询集合并根据孙辈的财产返回祖父母、父母和孙辈 例如,对于下面的对象,我只想在条件与计划的ID匹配时获取报价、费率父项和计划孙子项 public class Quote { public int Id { get; set; } public string Type { get; set; } public string ParentType { get; set; } public decimal ValueMax { get; se

为这个笨拙的标题道歉。我需要查询集合并根据孙辈的财产返回祖父母、父母和孙辈

例如,对于下面的对象,我只想在条件与计划的ID匹配时获取报价、费率父项和计划孙子项

public class Quote
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string ParentType { get; set; }
    public decimal ValueMax { get; set; }
    public virtual ICollection<Rate> Rates { get; set; }
}

public class Rate
{
    public int Id { get; set; }
    public decimal ValueMax { get; set; }
    public ICollection<Plan> Plans { get; set; }
}

public class Plan

    public int Id { get; set; }
    public decimal Price { get; set; }
}

您应该能够执行以下操作,以获得满足任何标准的计划,以及它们的父对象和祖父母对象:

// results is an anonymous type with properties Quote, Rate, Plan
var results = from q in quotes
    from r in q.Rates
    from p in r.Plans
    where p.Id < 500 /* whatever criteria */
    select new 
    {
        Quote = q,
        Rate = r,
        Plan = p
    };

您应该能够执行以下操作,以获得满足任何标准的计划,以及它们的父对象和祖父母对象:

// results is an anonymous type with properties Quote, Rate, Plan
var results = from q in quotes
    from r in q.Rates
    from p in r.Plans
    where p.Id < 500 /* whatever criteria */
    select new 
    {
        Quote = q,
        Rate = r,
        Plan = p
    };

谢谢你,戈里奇。我会继续看我之前的问题。谢谢Goric。我会继续看我之前的问题。谢谢Goric。这太完美了,比我想象的要简单得多。这太完美了,比我想象的简单多了