查询列表列表

时间:2014-08-26 16:01:23

标签: c# linq

假设我们有课程:

Class School
{
    List<Student> Students;
}

Class Student
{
    List<PhoneNumber> PhoneNumbers;
}

如何编写查询以获取学校中所有PhoneNumbers的列表?

2 个答案:

答案 0 :(得分:3)

一个简单的LINQ解决方案是使用SelectMany

School.Students.SelectMany(s => s.PhoneNumbers);

答案 1 :(得分:3)

使用Enumerable.SelectMany之类的:

List<PhoneNumber> allPhoneNumbers = school.Students
                                          .SelectMany(r => r.PhoneNumbers)
                                          .ToList();