A challenge for you LINQ experts
Imagine a simple, recursive parent-child relationship, modeled in data. The class definition would be:
class Relationship
{
public int ChildId { get; set; }
public int ParentId { get; set; }
}
You have a collection of these things. Let's define it as an IEnumerable and call it c.
The challenge:
1. Can you, with one LINQ query, find all the elements that are descendents of a given parent?
2. (Bonus points) Can you, with one LINQ query, find all "loops" in the collection, where an element is ultimately descended from itself?
class Relationship
{
public int ChildId { get; set; }
public int ParentId { get; set; }
}
You have a collection of these things. Let's define it as an IEnumerable
The challenge:
1. Can you, with one LINQ query, find all the elements that are descendents of a given parent?
2. (Bonus points) Can you, with one LINQ query, find all "loops" in the collection, where an element is ultimately descended from itself?
1 Comments:
can you share the answer for this?
Post a Comment
<< Home