Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:35:38+00:00 2026-05-17T17:35:38+00:00

Thanks to nHibernate, some of the data structures I work with are lists within

  • 0

Thanks to nHibernate, some of the data structures I work with are lists within lists within lists. So for example I have a data object called “category” which has a .Children property that resolves to a list of categories … each one of which can have children … and so on and so on.

I need to find a way of starting at a top-level category in this structure and getting a list or array or something similar of all the children in the entire structure – so all the children of all the children etc etc, flattened into a single list.

I’m sure it can be done with recursion, but I find recursive code a pain to work through, and I’m convinced there must be a more straightforward way in .Net 4 using Linq or somesuch – any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:35:39+00:00Added an answer on May 17, 2026 at 5:35 pm

    Assuming your Category class looks something like:

     public class Category
     {
       public string Name { get; set; }
       public List<Category> Children { get; set; }
     }
    

    I don’t think there’s an “easy” non-recursive way to do it; if you’re simply looking for a single method call to handle it, the “easy” way is to write the recursive version into a single method call. There’s probably an iterative way to do this, but I’m guessing it’s actually pretty complicated. It’s like asking the “easy” way to find a tangent to a curve without using calculus.

    Anyway, this would probably do it:

    public static List<Category> Flatten(Category root) {
    
        var flattened = new List<Category> {root};
    
        var children = root.Children;
    
        if(children != null)
        {
            foreach (var child in children)
            {
                flattened.AddRange(Flatten(child));
            }
        }
    
        return flattened;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance for your help. I have a need within an application to
I'm using NHibernate as my ORM and I'm trying to sort some data. The
I insert some data into a sql server 2005 database using NHibernate. Before I
I have a task to store large amount of gps data and some extra
I have some code which pulls records out of the database and I'm then
I am trying to mavenize my grails 2.1 app which uses build-test-data-2.0.3, for some
i have to retrive some data and count of the rows based on the
I am new to Fluent NHibernate and working with some relatively simple object associations.
I've got a simple pojo which is filled thanks to hibernate. I would like
Thanks to the epic work of Dennis Williamson, I am now able to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.