【问题标题】:UWP get values from nested List in IEnumerableUWP 从 IEnumerable 中的嵌套列表中获取值
【发布时间】:2021-04-20 15:59:39
【问题描述】:

我有这样的“部分”嵌套列表的列表:

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"
            },
        };
    }

我想要获得的是所有部分的列表,以便我可以搜索某个“名称”。到目前为止,我只能在第一层内搜索:

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

问题是,当我将其更改为以下内容时:

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

我收到一条错误消息,指出没有找到任何东西。

有没有办法实现这一点,还是我以错误的方式接近这一点?

非常感谢!!

【问题讨论】:

    标签: c# ienumerable


    【解决方案1】:

    听起来您想递归迭代您的部分树?你可以这样做,例如一个简单的深度优先搜索:

    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;
                }
            }
        }
    }
    

    然后,您可以使用它来获取所有部分的扁平列表,并搜索您所关注的部分

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2015-04-26
      • 2022-10-12
      • 2020-05-05
      • 2014-08-27
      • 2021-11-25
      相关资源
      最近更新 更多