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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:44:21+00:00 2026-05-27T15:44:21+00:00

I do recursion to find a long value within a List with multiple children

  • 0

I do recursion to find a long value within a List with multiple children that also can have children.

following method:

public TaxonomyData getTaxonomyData(long taxId, List<TaxonomyData> TaxonomyTree, TaxonomyData output)
{
    //find taxid in the taxonomy tree list and return the taxonomydata

    foreach (TaxonomyData td in TaxonomyTree)
    {
        if (td.TaxonomyId == taxId)
        {
                output = td;
                //return td; => when doing a return here means I already found a match so it is not necessary to do all the recursion.
        }
        else if (td.Taxonomy.Length > 0)
        {
            getTaxonomyData(taxId, td.Taxonomy.ToList(), output);
        }
    }

    return output;
}

Is it possible when I do return td; (see commented row) that my whole recursion stops?

Thanks

  • 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-27T15:44:22+00:00Added an answer on May 27, 2026 at 3:44 pm

    I suspect you want something like:

    public TaxonomyData GetTaxonomyData(long taxId, IEnumerable<TaxonomyData> tree)
    {
        foreach (TaxonomyData td in tree)
        {
            if (td.TaxonomyId == taxId)
            {
                return td;
            }
            else
            {
                // See if it's in the subtree of td
                TaxonomyData data = GetTaxonomyData(taxId, td.Taxonomy);
                if (data != null)
                {
                    return data;
                }
            }
        }
        // Haven't found it anywhere in this tree
        return null;
    }
    

    Each return only returns one level, but by checking the return value in the else clause, we can return all the way up the stack when we find the right value.

    The final result returned to the caller will be a null reference if it hasn’t been found.

    Note that I’ve removed the “output” parameter, which wouldn’t have been effective anyway as it wasn’t a ref parameter, and isn’t as clear as just using the return value.

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

Sidebar

Related Questions

Using recursion, find an index that cuts an array in two parts so that
I have a recursion function that parses an object/array with a global variable. If
I have the following function in my c# silverlight application to find the total
I have an rewrite recursion error somewhere on my website that Google Bot caused,
I want to find all excel files within a directory structure using recursion. The
I have an XSLT with recursion and I call the recursion within a for-each
What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path
How can I tell unix find to include in it's recursive search a folder
in regard to recursion what is a seed value?
I know that recursion is sometimes a lot cleaner than looping, and I'm not

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.