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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:14:09+00:00 2026-05-25T00:14:09+00:00

Within an asp.net application I have a list of categories objects, within this list

  • 0

Within an asp.net application I have a list of categories objects, within this list each category can be a parent of another category.

Example:

catid 1 catname cat1 parentid null
catid 2 catname cat2 parentid null
catid 3 catname cat3 parentid 2
catid 4 catname cat4 parentid 2
catid 5 catname cat5 parentid 4
catid 6 catname cat6 parentid 5
catit 7 catname cat7 parentid 5

I want to write a method that loops through the list of categories, pulls out the parent categories and acquires the child categories from the list.
Doing this is easy the hard part I am having problems with is how do I know when the last category object has been reached within a recursive method.

This is the logic I am looking for

protected void load_categories(ref List<category> list, category item)
{
     //loop through list and match item ID with list item parent ID
     //loop through child items of category item using load_categories()
     //HOW DO I STOP ONCE EVERYTHING IS DONE?
}
  • 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-25T00:14:09+00:00Added an answer on May 25, 2026 at 12:14 am

    I would do so:

    List<category> categoryWithParents = new List<category>();
    protected void load_categories(List<category> list, category item)
    {
        foreach(category cat in list)
        {
           if(item.id == cat.id)
           {
             categoryWithParents.Add(cat);
             if(cat.parentid != null) //if category has parent
               load_categories(list, cat); //load that parent
             break; //adding break should stop looping because we found category
           }
        }
    }
    

    when you call method with category catid 5 catname cat5 parentid 4 categoryWithParents list should contain(in adding order):

    catid 5 catname cat5 parentid 4    
    catid 4 catname cat4 parentid 2
    catid 2 catname cat2 parentid null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET application. It resides within another ASP.NET application. I put my
I have a form within an ASP.NET MVC application and I'm trying to submit
We have a ASP .Net application whereby we use Log4Net to log details within
How can I use the <label> tag within an ASP.NET application? I want it
I've discovered recently that when using the Session or Application objects within an ASP.net
How can I get hold of a user's GEO location within my ASP.NET application?
I have an asp net application where I use dropdown list. It is not
I have an ajax controltoolkit reorderlist within an asp.net application. I need to disable
I have a requirement for within an ASP.Net application to open a Word template
I have a Silverlight control that is hosted within an ASP.NET application. The Silverlight

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.