Sunday, November 15, 2009

Using linq to select nodes in tree structure

If we have tree structure of object like this:

class C1
public List Children {get; set;}
public string Name {get; set;}

and have list of C1 object for example "listOfC1" we can use Linq to select object that match specific name path, for example "N1/N2/N3":

var result =
from c1 in listOfC1
where c1.Name == "N1"
from c2 in c1.Children
where c2.Name == "N2"
from c3 in c2.Children
where c3.Name == "N3"
select c3;


Anonymous said...

Dear Author !
Completely I share your opinion. In it something is also idea good, agree with you.

Anonymous said...

I want to quote your post in my blog. It can?
And you et an account on Twitter?

Mijalko said...

Yes, you can.