Linq to Flatten results instead of recurrsion

3 posts, 0 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 13 May 2011 Link to this post

    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. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 19 May 2011 Link to this post

    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.
  3. DevCraft banner
  4. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 19 May 2011 Link to this post

    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;
     
            }
Back to Top