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 4092384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:29:05+00:00 2026-05-20T19:29:05+00:00

I have an entity called Category and the entity contains a IEnumerable called ChildCategories.

  • 0

I have an entity called Category and the entity contains a IEnumerable called ChildCategories. A category can have these child categories which can have it’s own child categories and so on.

Say I have selected out the top level parent category, I want to get all child categories and their child categories and so on so that I have all hierarchical children of the category. I want this flatterned and returned with the initial category. I’ve tried creating something like

    public static IEnumerable<T> AllChildren<T>(this IEnumerable<T> items, 
        Func<T, IEnumerable<T>> children, bool includeSelf)
    {
        foreach (var item in items)
        {
            if (includeSelf)
            {
                yield return item;
            }
            if (children != null)
            {
                foreach (var a in children(item))
                {
                    yield return a;
                    children(a).AllChildren(children, false);
                }
            }
        }
    }

Which would get flatterned after using SelectMany method but havn’t quite got it.

  • 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-20T19:29:06+00:00Added an answer on May 20, 2026 at 7:29 pm

    In his blog post Traverse a hierarchical structure with LINQ-to-Hierarchical
    , Arjan Einbu describes a method of flattening hierarchies for ease of querying:

    Can I make a generic extension method that will flatten any hierarchy? […]

    To do that, we need to analyze which parts of the method needs to be swapped out. That would be the TreeNode’s Nodes property. Can we access that in an other way? Yes, I think a delegate can help us, so lets give it a try:

    public static IEnumerable<T> FlattenHierarchy<T>(this T node, 
                                 Func<T, IEnumerable<T>> getChildEnumerator)
    {
        yield return node;
        if(getChildEnumerator(node) != null)
        {
            foreach(var child in getChildEnumerator(node))
            {
                foreach(var childOrDescendant 
                          in child.FlattenHierarchy(getChildEnumerator))
                {
                    yield return childOrDescendant;
                }
            }
        }
    }
    

    casperOne describes this in his answer as well, along with the problems inherent in trying to traverse the hierarchy directly using LINQ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract entity called Block which contains two attributes: column and order
I have a db.BlobProperty property (called Icon) in my entity which contains an image
I have an entity called User which can have a list of other Users
Lets say I have an entity called User which has many Posts. My service
I have a java application which uses JPA. Say I have an entity called
I have an entity called Band which has a to-many relationship to a Category
I have an entity object which contains two element collections as such: @Entity public
I have an entity called objectives, each objectives can have effects on other objectives,
I have a category called New which has 14 products within it. I'm using
I have an entity called Medico which was created as an Entity Class from

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.