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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:54:09+00:00 2026-05-20T19:54:09+00:00

I have a categories table which is set up to allow an infinite number

  • 0

I have a categories table which is set up to allow an infinite number of sub category levels. I would like to mimic the following:
enter image description here

It should be clarified that sub categories can have sub categories. E.g. Parent cat -> level 1 -> level 2 -> level 3 etc.

My categories table has two columns, CategoryName and ParentID.

This list box will be used when assigning the correct category to a product.

How can I write this?

Edit

In response to thedugas I had to modify your answer to work with my situation. I found some errors that needed to be fixed, but below is a final, working solution.

protected void Page_Load(object sender, EventArgs e)
    {
        using (DataClasses1DataContext db = new DataClasses1DataContext())
        {

        var c = db.Categories.Select(x => x);

        List<Category> categories = new List<Category>();
        foreach (var n in c)
        {
            categories.Add(new Category()
            {
                categoryID = n.categoryID,
                title = n.title,
                parentID = n.parentID,
                isVisible = n.isVisible
            });
        }
        List<string> xx = new List<string>();

        foreach (Category cat in categories)
        {
            BuildCatString(string.Empty, cat, categories, xx);
        }

        ListBox1.DataSource = xx;
        ListBox1.DataBind();

    }
}

private void BuildCatString(string prefix, Category cat, IEnumerable<Category> categories, List<string> xx)
{
    if (cat.parentID == 0)
    {
        xx.Add(cat.title);
        prefix = cat.title;
    }

    var children = categories.Where(x => x.parentID == cat.categoryID);
    if (children.Count() == 0)
    {
        return;
    }

    foreach (Category child in children)
    {
        if(prefix.Any())
        {
        xx.Add(prefix + "/" + child.title);
        BuildCatString(prefix + "/" + child.title,
            child, categories, xx);
        }
    }

}

Here is the almost finished work:
enter image description here

  • 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-05-20T19:54:10+00:00Added an answer on May 20, 2026 at 7:54 pm

    Nick asked me in a comment to another question how this sort of problem might be solved using LINQ to Objects without using any recursion. Easily done.

    Let’s suppose that we have a Dictionary<Id, Category> that maps ids to categories. Each category has three fields: Id, ParentId and Name. Let’s presume that ParentId can be null, to mark those categories that are “top level”.

    The desired output is a sequence of strings where each string is the “fully-qualified” name of the category.

    The solution is straightforward. We begin by defining a helper method:

    public static IEnumerable<Category> CategoryAndParents(this Dictionary<Id, Category> map, Id id)
    {
        Id current = id;
        while(current != null)
        {
            Category category = map[current];
            yield return category;
            current = category.ParentId;
        }
    }
    

    And this helper method:

    public static string FullName(this Dictionary<Id, Category> map, Id id)
    {
        return map.CategoryAndParents(id)
                  .Aggregate("", (string name, Category cat) =>
                    cat.Name + (name == "" ? "" : @"/") + name);
    }
    

    Or, if you prefer avoiding the potentially inefficient naive string concatenation:

    public static string FullName(this Dictionary<Id, Category> map, Id id)
    {
        return string.Join(@"/", map.CategoryAndParents(id)
                                    .Select(cat=>cat.Name)
                                    .Reverse());
    }
    

    And now the query is straightforward:

    fullNames = from id in map.Keys
                select map.FullName(id);
    listBox.DataSource = fullNames.ToList();
    

    No recursion necessary.

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

Sidebar

Related Questions

I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have a products table that contains a FK for a category, the Categories
Me again... I have an XML File that contains different categories which I would
I have a categories table, which one of the fields serves as the foreign
have a poll table which has 10 categories and I want to view the
I have a table with various fields 2 of which contain a number between
I have a MySQL table set up using phpMyAdmin which you can view in
I have a transactions table which contains a category ( category_id ), an amount
I have a set of data that models a hierarchy of categories. A root

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.