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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:09:33+00:00 2026-05-13T00:09:33+00:00

I have a List of objects of type IGroup. These can be nested to

  • 0

I have a List of objects of type IGroup. These can be nested to an umlimited level, and I’m trying to group them after retrieving them from a database. I can’t get my head around how to recursively add all groups to the right parents. Any groups with null as a parent are top level groups. I can’t guarantee the order they come out of the database.

public interface IGroup {
  string ID { get; set; }
  string Name { get; set; }
  string ParentID { get; set; }
  IList<IGroup> Groups { get; set; }
  ...

So if I had a list of:

Group1: ID = g1, ParentID = null
Group1a: ID = g2, ParentID = g1
Group2: ID = g3, ParentID = null
Group1b: ID = g4, ParentID = g3
Group1bc: ID = g5, ParentID = g4

I’m trying to group them as:

|Group1
|--Group1a
|--Group1b
|--|
   |--Group1bc
|Group2

Anyone fancy a stab at grouping them recursively?

  • 1 1 Answer
  • 1 View
  • 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-13T00:09:34+00:00Added an answer on May 13, 2026 at 12:09 am

    No need to be recursive. To wit:

    var lookup = items.ToDictionary(g => g.ID); // items is IEnumerable<IGroup>
    foreach (var item in items.Where(g => g.ParentID != null)) {
        lookup[item.ParentID].Groups.Add(item);
    }
    var parents = items.Where(g => g.ParentID == null);
    

    Note that lookup[item.ParentID] will throw if there is no IGroup with the corresponding ParentID. You can handle this more gracefully with TryGetValue.

    My implementation of IGroup:

    public class Group : IGroup {
        public string ID { get; set; }
        public string Name { get; set; }
        public string ParentID { get; set; }
        public IList<IGroup> Groups { get; set; }
        public Group() {
            Groups = new List<IGroup>();
        }
    }
    

    My test items:

    IEnumerable<IGroup> items = new List<IGroup>() {
        new Group() { ID = "g1", ParentID = null },
        new Group() { ID = "g2", ParentID = "g1" },
        new Group() { ID = "g3", ParentID = null },
        new Group() { ID = "g4", ParentID = "g3" },
        new Group() { ID = "g5", ParentID = "g4" },
        new Group() { ID = "g6", ParentID = "g5" }
    };    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects, can be of any type T . How
Greetings, I have some List of objects of Type CMessage. CMessage can look as
I have a list of objects of type A. In a first iteration I
I have list of objects type Node ( items ). And Node class has
I have a list of objects of type C, where type C consists of
I have a list containing objects of type numpy.ndarray, all list elements has same
I have a list with objects of x type. Those objects have an attribute
I have a list of objects that I am trying to bind to a
I have a list of objects (of type 'TrackedSet') that is data bound to
Let's assume I have a list with objects of type Value . Value has

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.