如何使用 linq 在 C# 中获取列表列表的元素?

How to get elements of a List of Lists in C# using linq?

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

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

SelectMany()

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

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

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