Search code examples
c#ienumerable

UWP get values from nested List in IEnumerable


I have List with nested Lists of "Sections" like this:

private static IEnumerable<Section> Menu()
    {
        return new List<Section>()
        {
            new Section()
            {
                Name = "S1",
                Submenu = new List<Section>()
                {
                    new Section()
                    {
                        Name="S1A",
                    },
                    new Section()
                    {
                        Name="S1B",
                        Submenu = new List<Section>()
                        {
                            new Section()
                            {
                                Name = "S1BA",
                            },
                            new Section()
                            {
                                Name = "S1BB",
                            },
                            new Section()
                            {
                                Name = "S1BC",
                            }
                        }
                    },
                    new Section()
                    {
                        Name="S1C",
                    },
                    new Section()
                    {
                        Name="S1D",
                    },
                }
            },
            new Section()
            {
                Name = "S2",
                Submenu = new List<Section>()
                {
                    new Section()
                    {
                        Name = "S2A",
                    },
                    new Section()
                    {
                        Name = "S2B",
                    }
                }
            },
            new Section()
            {
                Name = "S3"
            },
        };
    }

What I'm trying to get is a list of all the sections so I can search for a certain "Name". So far I can only search within the first layer with:

string sectionName = "S1"
Section _section = Menu().First(u => u.Name == sectionName);

The problem is that when I change it to something like:

string sectionName = "S1BC"
Section _section = Menu().First(u => u.Name == sectionName);

I get an error stating that nothing was found.

Is there a way to achieve this or am I approaching this in a wrong way?

Thanks a lot!!


Solution

  • It sounds like you want to recursively iterate your tree of sections? You can do this with e.g. a simple depth-first search:

    private static IEnumerable<Section> Flatten(IEnumerable<Section> sections)
    {
        foreach (var section in sections)
        {
            // Yield the section itself
            yield return section;
    
            // Visit the section's subsections, and yield them all
            if (section.Submenu != null)
            {
                foreach (var subsection in Flatten(section.Submenu))
                {
                    yield return subsection;
                }
            }
        }
    }
    

    You can then use this to get a flattened list of all sections, and search that for the one you're after

    string sectionName = "S1BC"
    Section _section = Flatten(Menu()).First(u => u.Name == sectionName);