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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:31:45+00:00 2026-06-04T20:31:45+00:00

I am using C# language. My problem is that i don’t know how to

  • 0

I am using C# language. My problem is that i don’t know how to store my retrieved hierarchical result set to my object.

Here’s is my Object:

public class CategoryItem
{
    public string Name { get; set; }
    public int CategoryID { get; set; }
    public int ParentID { get; set; }
    public List<CategoryItem> SubCategory = new List<CategoryItem>();
    public List<CategoryItem> GetSubCategory()
    {
        return SubCategory;
    }
    public void AddSubCategory(CategoryItem ci)
    {
        SubCategory.Add(ci);
    }
    public void RemoveSubCategory(CategoryItem ci)
    {
        for (int i = 0; i < SubCategory.Count; i++)
        {
            if (SubCategory.ElementAt(i).CategoryID == ci.CategoryID)
            {
                SubCategory.RemoveAt(i);
                break;
            }
        }
    }
}

Here’s is my sample retrieve data set from MSSQL server

ID  PrntID  Title   
_______ _______     
1   0   Node1   
2   1   Node2   
3   1   Node3   
4   2   Node4   
5   2   Node5   
6   2   Node6   
7   3   Node7   
8   4   Node8   
9   4   Node9   
10  9   Node10

Tree view for easy reference

Node 1
-Node 2
--Node 4
---Node 8
---Node 9
----Node 10
--Node 5
--Node 6
-Node 3
--Node 7

My problem is how to do I store this result to my “CategoryItem Object”. I don’t have any clue do I need to use iteration for this? Specially when the node is 2 level-deep.
I want to store it in such a like this:

List<CategoryItem> items = new List<CategoryItem>();

with this I can dig every objects in the ‘items’ object and I can access its sub-category / child / children using the GetSubCategory() method of my class. Is this possible?

  • 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-04T20:31:46+00:00Added an answer on June 4, 2026 at 8:31 pm

    If you know that in your DataSet a node will never appear before its parent, you can use this code. Here you keep track of the already read items in a Dictionary when you can look for parents of the newly read nodes. If you find the parent you add the new item to its children, otherwise it’s a first level node.

        public static List<CategoryItem> LoadFromDataSet(DataSet aDS)
        {
            List<CategoryItem> result = new List<CategoryItem>();
            Dictionary<int, CategoryItem> alreadyRead = new Dictionary<int, CategoryItem>();
            foreach (DataRow aRow in aDS.Tables["YourTable"].Rows)
            {
                CategoryItem newItem = new CategoryItem();
                newItem.CategoryID = (int)aRow["ID"];
                newItem.ParentID = (int)aRow["PrntID"];
                newItem.Name = (string)aRow["Title"];
                alreadyRead[newItem.CategoryID] = newItem;
                CategoryItem aParent;
                if (alreadyRead.TryGetValue(newItem.ParentID, out aParent))
                    aParent.AddSubCategory(newItem);
                else
                    result.Add(newItem);
            }
            return result;
        }
    

    If my assumption isn’t true (i.e. it is possible for a node to appear in the DataSet before its parent), you have to first read all the nodes (and put them in the Dictionary), then loop through the same Dictionary to build the result. Something like this:

        public static List<CategoryItem> LoadFromDataSet(DataSet aDS)
        {
            List<CategoryItem> result = new List<CategoryItem>();
            Dictionary<int, CategoryItem> alreadyRead = new Dictionary<int, CategoryItem>();
            foreach (DataRow aRow in aDS.Tables["YourTable"].Rows)
            {
                CategoryItem newItem = new CategoryItem();
                newItem.CategoryID = (int)aRow["ID"];
                newItem.ParentID = (int)aRow["PrntID"];
                newItem.Name = (string)aRow["Title"];
                alreadyRead[newItem.CategoryID] = newItem;
            }
            foreach (CategoryItem newItem in alreadyRead.Values)
            {
                CategoryItem aParent;
                if (alreadyRead.TryGetValue(newItem.ParentID, out aParent))
                    aParent.AddSubCategory(newItem);
                else
                    result.Add(newItem);
            }
            return result;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating xml-like mark-up language using System.Xml.XmTextWriter that will be read by a third
I have a book, Essential ActionScript 3 (O'Reilly), to learn about using that language.
I'm makin' a scripting language interpreter using PHP. I have this code in that
I'm a moderate fan of using Exception subtypes to help describe the problem that
I don't know if you know excel-dna project, it's a project that help to
A common problem in any language is to assert that parameters sent in to
We're considering using Python (IronPython, but I don't think that's relevant) to provide a
My specific problem is that I have a set of Apache access logs, and
I'm using language files to allow users to switch between English and Dutch. The
I started using LINQ (Language Integrated Query) when it was still in beta, more

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.