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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:10:17+00:00 2026-06-09T13:10:17+00:00

Let’s say we have a Windows app that has a TreeView and you can

  • 0

Let’s say we have a Windows app that has a TreeView and you can expand the nodes of this view and drill down to the children node, they may also have more children so now we can expand that node and go further, etc.. So in my source code I have a method Foo(string fatherNode) that gets the father node that we clicked on and finds the children and lists them:

the high level body of this method is like this:

private void Foo(string fatherNode)
{
  // call some DB scripts and grab data you need to work with.
  int numberOfKids = // get it from the thing you populated from the DB call.
  for(int i = 1  to numberOfKids)
  {
     Node Child = // grab child[i] from the list we populated from DB calls
     //Add it to the treeView
  }
}

Well that code is good for a UI app, where we click on a node, it runs this method once and collects the data it needs, Now I need to write another method utilizing useful lines of the method above Grab EVERYTHING at once and write the Whole information to let’s say a file.

So in my head in looks like a recursive method to me. But still can’t figure out the whole picture, Prob should have two collections, one for fathers, one for kids, loop through kids and make recursive call to get more kids and add them to the collection, etc

I was wondering if you can clear out the fogs, the high-level of what I need to do to, how the colections should look like, where to add to them, where to call the recursive method call, etc…and please don’t specifically think about a “treeview” object, I just used that as an example to expalin the question better. The main thing is the structure of the Foo method that I posted. That’s what I should work with.

  • 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-09T13:10:19+00:00Added an answer on June 9, 2026 at 1:10 pm

    Well, I’m not sure if this is what you’re looking for, even after the other answers.
    However, please, check it out:

    The self-related entity (Node)

    public class MyEntity
    {
        public MyEntity() { }
    
        public MyEntity(string Name, int ID, int? ParentID)
        {
            this.Name = Name;
            this.ID = ID;
            this.ParentID = ParentID;
        }
    
        public string Name { get; set; }
        public int ID { get; set; }
        public int? ParentID { get; set; }
    }
    

    The tree building methods

        public static StringBuilder GetFamilyTree(List<MyEntity> AllTheEntities)
        {
            StringBuilder Return = new StringBuilder();
    
            List<MyEntity> OrderedEntities = AllTheEntities.OrderBy<MyEntity, int>(x => x.ID).ToList();
    
            foreach (MyEntity CurrentEntity in AllTheEntities.Where<MyEntity>(x => !x.ParentID.HasValue))
            {
                Return.AppendLine(GetEntityTree(AllTheEntities, CurrentEntity));
            }
    
            return Return;
        }
    
        public static string GetEntityTree(List<MyEntity> AllTheEntities, MyEntity CurrentEntity, int CurrentLevel = 0)
        {
            StringBuilder Return = new StringBuilder();
    
            Return.AppendFormat("{0}{1}", "\t".Repeat(CurrentLevel), CurrentEntity.Name);
            Return.AppendLine();
    
            List<MyEntity> Children = AllTheEntities.Where<MyEntity>(x => x.ParentID.HasValue && x.ParentID.Value == CurrentEntity.ID).ToList();
    
            if (Children != null && Children.Count > 0)
            {
                foreach (MyEntity CurrentChildEntity in Children)
                {
                    Return.Append(GetEntityTree(AllTheEntities, CurrentChildEntity, CurrentLevel + 1));
                }
            }
    
            return Return.ToString();
        }
    

    A small helper class

    public static class StringExtension
    {
        public static string Repeat(this string text, int times)
        {
            string Return = string.Empty;
    
            if (times > 0)
            {
                for (int i = 0; i < times; i++)
                {
                    Return = string.Concat(Return, text);
                }
            }
    
            return Return;
        }
    }
    

    Usage

            List<MyEntity> AllMyEntities = new List<MyEntity>();
            AllMyEntities.Add(new MyEntity("1", 1, null));
            AllMyEntities.Add(new MyEntity("1.1", 2, 1));
            AllMyEntities.Add(new MyEntity("1.1.1", 3, 2));
            AllMyEntities.Add(new MyEntity("2", 4, null));
            AllMyEntities.Add(new MyEntity("2.1", 5, 4));
    
            Console.Write(GetFamilyTree(AllMyEntities).ToString());
    

    Results

    1
        1.1
            1.1.1
    2
        2.1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say that I have a set of relations that looks like this: relations
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a

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.