This is a migrated thread and some comments may be shown as answers.

Linq to Flatten results instead of recurrsion

2 Answers 259 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 13 May 2011, 10:11 PM
Ok so I've got a table with parent\child relationships on the items

What I want to have in my partial class is a method to flatten all the children of the current node and any of it's children out into a List<int>.

Is that somehow doable with Linq, or do I have to loop through each item and it's children?

Thanks,
Steve

2 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 19 May 2011, 10:28 AM
Hi Steve,

 Can you be more specific by describing us your model and what you would like to achieve. I am pretty sure that it should be achievable with Linq but I am not sure I understand your idea about flattening out a persistent object and its children objects into a List<int>?

All the best,
Zoran
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 19 May 2011, 02:32 PM
No need, I found an awesomely simple method to do this

public IEnumerable<CommonProgram> FlattenedChildProgramList(CommonProgram parent) {
            yield return parent;
            foreach (CommonProgram child in parent.ChildPrograms) // check null if you must
                foreach (CommonProgram relative in FlattenedChildProgramList(child))
            yield return relative;
 
        }
Tags
LINQ (LINQ specific questions)
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Zoran
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Share this question
or