简体   繁体   English

UWP 从 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!!非常感谢!!

It sounds like you want to recursively iterate your tree of sections?听起来您想递归地迭代您的部分树? You can do this with eg 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM