Reputation: 1337
Apologies for the clunky title. I need to query a collection and return the grandparent, parent and grandchild based on a grandchild's property
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; }
}
Upvotes: 0
Views: 853
Reputation: 11895
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
};
Upvotes: 4