Linq查询根据孙子的属性返回祖父母,父母和孙子 [英] Linq query to return grandparent, parent and grandchild based on a grandchild's property

查看:82
本文介绍了Linq查询根据孙子的属性返回祖父母,父母和孙子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为笨拙的标题表示歉意.我需要查询一个集合,并根据孙子的属性返回其祖父母,父母和孙子.

Apologies for the clunky title. I need to query a collection and return the grandparent, parent and grandchild based on a grandchild's property

例如,对于下面的对象,仅当条件与计划的ID匹配时,我才想获取报价,费率(父项)和计划(GrandChild).

For example with the object below, I want to get the Quote, the Rate (Parent) and the Plan (GrandChild) only if a condition matches the Plan's 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; }
}

推荐答案

您应该能够执行以下操作,以获取符合任何条件的计划以及其父对象和祖父母对象:

You should be able to do something like this to get the plans which meet whatever criteria, along with their parent and grandparent objects:

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

这篇关于Linq查询根据孙子的属性返回祖父母,父母和孙子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆