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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:33+00:00 2026-06-17T05:55:33+00:00

I have been stuck on this problem for a few days and would appreciate

  • 0

I have been stuck on this problem for a few days and would appreciate some ideas or help in resolving it.
I have a collection of objects

 public class Hierarchy
{
    public Hierarchy(string iD, string name, int level, string parentID, string topParent)
    {
        ID = iD;
        Name = name;
        Level = level;
        ParentID = parentID;
        Children = new HashSet<Hierarchy>();
    }
    public string ID { get; set; }
    public string Name{ get; set; }
    public int Level { get; set; }
    public string ParentID { get; set; }
    public ICollection<Hierarchy> Children { get; set; }
}

The data from the Linq Query to my Entity is:

ID      Name     Level ParentID
295152  name1    1     null
12345   child1   2     295152
54321   child2   2     295152
44444   child1a  3     12345
33333   child1b  3     12345
22222   child2a  3     54321
22221   child2b  3     54321
22002   child2c  3     54321
20001   child2a2 4     22222
20101   child2b2 4     22222

This data could extend to an unknown depth of levels (I’m only showing 4).
Ultimately I would have one Hierarchy object with a collection of multiple child objects which in turn may have a collection of multiple child objects…etc…
There will always only be one top level object.

I am trying to use Linq as much as possible in this project.

This obviously needs some sort of recursive method but I’m stuck. Any ideas or help would be appreciated.

TIA

  • 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-06-17T05:55:34+00:00Added an answer on June 17, 2026 at 5:55 am

    You can try this recursive function:

    void PopulateChildren(Hierarchy root, ICollection<Hierarchy> source)
    {
        foreach (var hierarchy in source.Where(h => h.ParentID == root.ParentID))
        {
            root.Children.Add(hierarchy);
            PopulateChildren(root, source);
        }
    }
    

    Which you can use like this:

    ICollection<Hierarchy> hierarchies = new List<Hierarchy>(); // source
    
    // Get root
    var root = hierarchies.Single(h => h.Level == 1);
    
    // Populate children recursively
    PopulateChildren(root, hierarchies);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been stuck on this for a few days and I would ping
Have been stuck with this issue for a few days now, and really need,
Morning, I have been working on this problem for a few days and cannot
I have been trying to sort this problem out for the last few days.
I have been stuck on this issue for a few days now and seem
Quick note: I've been stuck with this problem for quite a few days and
Hope to get solution to this problem. I have been stuck on it since
I have been stuck on this for days, and was wondering if anyone had
I have now been stuck at this for some time so I though to
i know this is basic but somehow i have been stuck here for some

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.