'Recursive' LINQ calls

Posted by Sir Psycho on Stack Overflow See other posts from Stack Overflow or by Sir Psycho
Published on 2010-04-25T12:18:51Z Indexed on 2010/04/25 12:23 UTC
Read the original article Hit count: 181

Filed under:
|

Hi,

I'm trying to build an XML tree of some data with a parent child relationship, but in the same table.

The two fields of importance are

CompetitionID ParentCompetitionID

Some data might be

CompetitionID=1, ParentCompetitionID=null

CompetitionID=2, ParentCompetitionID=1

CompetitionID=3, ParentCompetitionID=1

The broken query I have simply displays results in a flat format. Seeing that I'm working with XML, some sort of recursive functionality is required. I can do this using recursion, but would like to see the linq version. Any help appreciated.

var results = 
        from c1 in comps
        select new {
            c.CompetitionID,
            SubComps=
                from sc in comps.Where (c2 => c2.CompetitionID == c1.CompetitionID)
                select sc
        };

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ