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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:44:18+00:00 2026-05-23T14:44:18+00:00

How could I change the following method to a recursive method: public List<ScanItem> search(List<ScanItem>

  • 0

How could I change the following method to a recursive method:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                        /// repeat until there are no more direcotries

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

let me show you how I built it:

Step1:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);

            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Step2:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);

                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Step 3:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

step 4:

    public List<ScanItem> search(List<ScanItem> allItems)
    {
        List<ScanItem> rt = new List<ScanItem>();

        foreach (ScanItem s in allItems)
        {
            if (s.IsDirectory)
            {
                rt.Add(s);



                foreach (ScanItem s1 in ((ScanDir)s).Items)
                {
                    if (s1.IsDirectory)
                    {
                        rt.Add(s1);




                        foreach (ScanItem s2 in ((ScanDir)s1).Items)
                        {
                            if (s2.IsDirectory)
                            {
                                rt.Add(s2);

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }

                            }
                            else
                                if (s2.Name.Contains("Foo"))
                                    rt.Add(s2);
                        }



                    }
                    else
                        if (s1.Name.Contains("Foo"))
                            rt.Add(s1);
                }


            }
            else                
                if (s.Name.Contains("Foo"))                    
                    rt.Add(s);                    

        }

        return rt;

    }

Note that I just keep copying:

                                foreach (ScanItem s3 in ((ScanDir)s2).Items)
                                {
                                    if (s3.IsDirectory)
                                    {
                                        rt.Add(s3);

                                        /// repeat until there are no more direcotries

                                    }
                                    else
                                        if (s3.Name.Contains("Foo"))
                                            rt.Add(s3);
                                }
  • 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-23T14:44:18+00:00Added an answer on May 23, 2026 at 2:44 pm
    private void SearchCore(IEnumerable<ScanItem> items, List<ScanItem> foundItems)
    {
        foreach (ScanItem s in items)
        {
            if (s.IsDirectory)
            {
                foundItems.Add(s);
                searchCore (((ScanDir)s2).Items, foundItems);
            }
            else if (s.Name.Contains("Foo"))                    
                    foundItems.Add(s);
        }
    }
    
    public List<ScanItem> Search(List<ScanItem> allItems)
    {
        List<ScanItem> foundItems = new List<ScanItem>();
        searchCore (allItems, foundItems);
        return foundItems;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following get method: public List<PersonalMessage> getMessagesList() { List<PersonalMessage> newList = new
Here's a puzzle for you. I want to change the following comparison method, so
Given the following classes and controller action method: public School { public Int32 ID
I thought I could change the Boolean true/false value, but it's not working. How
I know with ASP.NET there's a global handler Application_EndRequest handler where I could change
How could I change the look and feel of standard UIKeyboard object. I want
How could I change the text below so that the text within it has
Could you let me know how can I change font size of button caption
Could someone explain to me in simple terms the easiest way to change the
Hey, I'm just wondering what code I could use to auto change the size

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.