使用Linq从包含List的List中的List中进行选择

Ash*_*ine 1 linq lambda select any

我有语法问题.

public class Student
{
   int StudentId;
   string Name;
}

public class Course
{
   int CourseId;
   List<Student> Students;
}


int[] studentIds = { 5, 7, 12 };
List<Course> allCourses = myDataContext.Courses.ToList();
Run Code Online (Sandbox Code Playgroud)

使用Lambda表达式查询表达式,如何获得包含数组中任何学生的所有课程的筛选列表studentIds

Joa*_*son 5

var result = from course in allCourses
             where course.Students.Any(x => studentIds.Contains(x.StudentId))
             select course;
Run Code Online (Sandbox Code Playgroud)