使用Linq从列表中选择列表中包含
问题描述:
我有语法麻烦。使用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
在任何学生的所有课程的过滤列表?
答
var result = from course in allCourses
where course.Students.Any(x => studentIds.Contains(x.StudentId))
select course;