【问题标题】:How to get elements of a List of Lists in C# using linq?如何使用 linq 在 C# 中获取列表列表的元素?
【发布时间】:2022-01-22 16:32:43
【问题描述】:

我有List<List < Address>>,我正在尝试从父列表中检索子列表中的元素,下面是我所做的,但这不是我想要实现的目标

var getChildElement = ParentList
        .Select(x => x.Select(y => y)
                      .Where(z => z.Stud.Res.StudentId == 54));

【问题讨论】:

标签: c# linq


【解决方案1】:

SelectMany()

将序列的每个元素投影到 IEnumerable 和 将生成的序列扁平化为一个序列

var getChildElement = ParentList.SelectMany(x => x.Stud.Res.StudentId == 54);

如文档中所述,SelectMany() 将您的 List<List<Address>> 扁平化为一个序列,我们的谓词将该序列过滤到输出中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2014-12-16
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多