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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:05:00+00:00 2026-06-17T15:05:00+00:00

I have two methods to return a dynamic hierarchical structure from a flat List.

  • 0

I have two methods to return a dynamic hierarchical structure from a flat List. The first works great using the recursive method here: (ID/ParentID) list to Hierarchical list.

I’m now trying to do the same thing except this time show only those categories and reports which have a saved report output. I’m not sure where to start as everything I find is building from root down and I need to go from the bottom up.

I get something like this now in my first method:

Category 1
  |_Sub Category 1
    |_Report 1
    |_Report 2
      |_Saved Output
Category 2
  |_Sub Category 2
  | |_Report 3
  | |_Report 4
  |_Sub Category 3
    |_Report 5
    |_Report 6
      |_Saved Output
Category 3
  |_Sub Category 4
    |_Report 7

What I want in my second method is this:

Category 1
  |_Sub Category 1
    |_Report 2
      |_Saved Output
Category 2
  |_Sub Category 3
    |_Report 6
      |_Saved Output

Here’s my basic test structure:

class Flat
{
    public int id { get; set; }
    public int parentId { get; set; }
    public string name { get; set; }
    public bool isOutput { get; set; }

    public Flat(int i, int pid, string n, bool o)
    {
        this.id = i;
        this.parentId = pid;
        this.name = n;
        this.isOutput = o;
    }
}

class MyClass
{
    public int id { get; set; }
    public int parentId { get; set; }
    public string name { get; set; }
    public bool isOutput { get; set; }

    public List<MyClass> children { get; set; }

    public MyClass()
    {
        this.children = new List<MyClass>();
    }
}

List<Flat> items = new List<Flat>()
        {
                new Flat(1,0,"Category 1",false),
                new Flat(4,1,"Sub Category 1",false),
                new Flat(8,4,"Report 1",false),
                new Flat(9,4,"Report 2",false),
                new Flat(15,9,"Saved Output",true),
                new Flat(2,0,"Category 2",false),
                new Flat(5,2,"Sub Category 2",false),
                new Flat(10,5,"Report 3",false),
                new Flat(11,5,"Report 4",false),
                new Flat(6,2,"Sub Category 3",false),
                new Flat(12,6,"Report 5",false),
                new Flat(13,6,"Report 6",false),
                new Flat(16,13,"Saved Output",true),
                new Flat(3,0,"Category 3",false),
                new Flat(7,3,"Sub Category 4",false),
                new Flat(14,7,"Report 7",false)
        };
  • 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-17T15:05:01+00:00Added an answer on June 17, 2026 at 3:05 pm

    To build from the bottom up, you need to start with all the leaf nodes that are valid (output == true), and then work upwards through all parent nodes until you reach the root. Here’s one method that should work:

    List<Flat> GetSavedOutput(List<Flat> items)
    {
        // get all output leaf nodes
        var toAdd = items.Where (i => i.isOutput == true).ToList();
        var result = new List<Flat>();
    
        // grab all parent nodes that are not already included until
        // there's nothing new to add
        while (toAdd.Count > 0)
        {
            result.AddRange(toAdd);
            toAdd = items.Where (i => !result.Contains(i)
                                   && result.Any (r => r.parentId == i.id)).ToList();
        }
    
        return result;
    }
    

    This is short and quick, and should work well for small, simple trees, but it is not the most efficient method because of processing the same nodes over and over again. A slightly more complex, but better method, would be to walk up the parent tree for each item:

    List<Flat> GetSavedOutput(List<Flat> items)
    {
        var savedOutput = items.Where (i => i.isOutput == true).ToList();
        var result = new List<Flat>();
    
        foreach (var item in savedOutput) {
            result.Add(item);
            var temp = item;
            do {
                temp = items.Single (i => i.id == temp.parentId);
                result.Add(temp);
            } while (temp.parentId != 0);
        }
    
        return result;
    }
    

    If this is still not efficient enough, you can get a little more performance by storing references to the parent node in each Flat instance, so that the parent can be directly referenced in O(1) without having to look it up using a call to Single, which has efficiency O(n).

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

Sidebar

Related Questions

i have tried implement two methods recursive and dynamic method and both took 0
I have two methods, the first needs a Map<ItemA, ItemB> the second a Map<ItemA
I have two methods that both return an IObservable IObservable<Something[]> QueryLocal(); and IObservable<Something[]> QueryWeb();
I have a method which compares two objects and returns a list of all
I have two methods declared public void MethodA(object o, Action<string> action) { } public
I have two methods -a and -b. -a calls sometimes -b, and -b sometimes
I have two methods, one called straight after another, which both throw the exact
I have two methods, one that I use to convert an image to a
lets say I have two methods in my controller to support both json and
Is it possible to have two methods with the same name but different parameters

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.