Wednesday, March 10, 2010

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?

1 Comments:

Blogger Ani said...

can you share the answer for this?

5/07/2010 12:14 AM  

Post a Comment

<< Home