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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:59:36+00:00 2026-06-05T03:59:36+00:00

I have this structure of List<Object> , to be specific it is a CategoryItem

  • 0

I have this structure of List<Object>, to be specific it is a “CategoryItem” Object. Here’s my declaration of “CategoryItem” Object.

public class CategoryItem
{
    public string Name { get; set; }
    public int CategoryID {get; set;}
    public int ParentID {get; set; }
    public List<CategoryItem> SubCategory {get; set;}
}

Here’s my sample data structure:

[0] CategoryID: 249
Name: "WelcomeNC"
ParentID: 0
SubCategory: length=4
    [0] CategoryID: 250
        Name: "CNC"
        ParentID: 249
        SubCategory: length=0
    [1] CategoryID: 251
        Name: "Production"
        ParentID: 249
        SubCategory: length=0
    [2] CategoryID: 252
        Name: "Administrative"
        ParentID: 249
        SubCategory: length=1
            [0] CategoryID: 261
                Name: "NPower"
                ParentID: 252
                SubCategory: length=0
    [3] CategoryID: 253
        Name: "Help"
        ParentID: 249
        SubCategory: length=1
            [0] CategoryID: 254
                Name: "7"
                ParentID: 253
                SubCategory: length=1
                    [0] CategoryID: 255
                        Name: "CHLK"
                        ParentID: 254
                        SubCategory: length=0
[1] CategoryID: 111
Name: "First"
ParentID: 0
SubCategory: length=0

My problem is, how do I step in each and every ‘CategoryItem’ object of my declared:

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

So that I can display it in an unordered list in html like this

  • WelcomeNC
    • CNC
    • Production
    • Administrative
      • NPower
    • Help
      • 7
        • CHLK
  • First

Is there a way to do this?

  • 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-05T03:59:38+00:00Added an answer on June 5, 2026 at 3:59 am

    If your CategoryItem doesn’t contain a list of its children (like in the first version of the question), I would first of all build a Dictionary that foreach CategoryID gives you all the subcategory items, then recursively print all your items using this dictionary and starting with the items with parent “0”. Assuming that Print is the instruction that print the data associated with your item, and that it takes as the only parameter the level of indentation, the code will look like this:

        public static void PrintItems(List<CategoryItem> items)
        {
            Dictionary<string, List<CategoryItem>> dictOfChildren = new Dictionary<string, List<CategoryItem>>();
            // loop through all the items grouping them according to their ParentID
            foreach (CategoryItem anItem in items)
            {
                List<CategoryItem> children;
                if (!dictOfChildren.TryGetValue(anItem.ParentID, out children))
                {
                    children = new List<CategoryItem>();
                    dictOfChildren[anItem.ParentID] = children;
                }
                children.Add(anItem);
            }
            // recursively print all the items starting from the ones with ParentID = 0
            // the dictionary is passed to the method in order to be able to find the children of each item
            PrintItems(dictOfChildren["0"], dictOfChildren, 0);
        }
    
        private static void PrintItems(List<CategoryItem> list, Dictionary<string, List<CategoryItem>> dictOfChildren, int levelOfIndentation)
        {
            foreach (CategoryItem anItem in list)
            {
                // first print the current item
                anItem.Print(levelOfIndentation);
                // then recursively print all its children
                List<CategoryItem> children;
                if (dictOfChildren.TryGetValue(anItem.CategoryID, out children) &&
                    children.Count > 0)
                    PrintItems(children, dictOfChildren, levelOfIndentation + 1);
            }
        }
    

    It’s not really object oriented, but this should give you a hint about the direction to follow.

    EDIT:

    I saw that you edited the question and that now you have added the SubCategory property. This makes things much simpler and you can simply do:

    public static void PrintItems(List<CategoryItem> items)
    {
        // call a recursive method passing 0 as level of indentation
        PrintItems(items, 0);
    }
    
    public static void PrintItems(List<CategoryItem> items, int levelOfIndentation)
    {
        foreach (CategoryItem anItem in items)
        {
            // print the currentItem
            anItem.Print(levelOfIndentation);
            // increment the level of indentation and callk the same method for the children
            PrintItems(anItem.SubCategory, levelOfIndentation + 1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have this structure: public class Parent { public string Name{get; set;} public
If I have this structure: public class Parent { public string Name{get; set;} public
I have 2 classes with this structure: class ClassA { String typeA; List<String> valuesA;
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have a structure that roughly looks like this: List<ProductLine> -> ID Name ...
I have this structure of classes: public class L3Message { public int Number {
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have this structure on form, <input type=test value= id=username /> <span class=input-value>John Smith</span>
Suppose i have this structure of elements: <div class=parent> <div class=something1> <div class=something2> <div
I have this HTML structure (excerpt): <td> <select id=test1 class=pub_chooser> <option value=99>All Publications</option> <option

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.