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

  • Home
  • SEARCH
  • 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 375529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:30:40+00:00 2026-05-12T14:30:40+00:00

I have a recursive method that returns categories, and checks for its sub categories.

  • 0

I have a recursive method that returns categories, and checks for its sub categories.

This is my code:

public List<Category> GetAllChildCats(int categoryid) 
{
    List<Category> list = new List<Category>();

    Category c = Get(categoryid);

    foreach(Category cat in c.ChildCategories) 
    {
        list.Add(GetAllChildCats(cat.CategoryID));
    }

}

This fails because the call to list.Add() expects a Category object, but GetAllChildCats() returns List<Category>

How should I work around 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-05-12T14:30:41+00:00Added an answer on May 12, 2026 at 2:30 pm

    Currently you haven’t shown anything which actually adds a single category to the list… I’m assuming that as you recurse, you want to add the results of Get(categoryId) as well·

    Preet’s solution will certainly work, but here’s an alternative which avoids creating all the extra lists:

    public List<Category> GetAllChildCats(int categoryId)
    {
        List<Category> ret = new List<Category>();
        GetAllChildCats(categoryId, ret);
        return ret;
    }
    
    private void GetAllChildCats(int categoryId, List<Category> list)
    {
        Category c = Get(categoryid);
        list.Add(c);
    
        foreach(Category cat in c.ChildCategories)
        {
            GetAllChildCats(cat.CategoryID, list);
        }
    }
    

    This creates a single list, and adds items to it as it goes.

    One point though – if you’ve already got the child Category objects, do you really need to call Get again? Does each child only contain its ID until you fetch the whole category?

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

Sidebar

Ask A Question

Stats

  • Questions 182k
  • Answers 182k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ASP.NET is actually not specifically bound to IIS. The .NET… May 12, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer Ahh the joys of extending the built-ins. for..in checks up… May 12, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer gmpy to the rescue...: >>> import gmpy >>> x =… May 12, 2026 at 4:24 pm

Related Questions

I'm fairly new to Python and recursive functions as a whole, so pardon my
Below is a method that I want to ship off into a background worker
I'm pretty new to the idea of recursion and this is actually my first
So it's the third week of my life in C and I've been given
Where I work we're still on .Net 2.0, but I'm somewhat familiar with the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.