2012-01-12 53 views
1

我有语法麻烦。使用Linq从列表中选择列表中包含

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(); 

使用lambda表达式查询表达式,如何获取包含数组studentIds在任何学生的所有课程的过滤列表?

回答

5
var result = from course in allCourses 
      where course.Students.Any(x => studentIds.Contains(x.StudentId)) 
      select course;