Question

I'm having syntax troubles.

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

Using Lambda expressions or query expressions, how do I get a filtered list of all the Courses containing any of the Students in the array studentIds?

Was it helpful?

Solution

var result = from course in allCourses
             where course.Students.Any(x => studentIds.Contains(x.StudentId))
             select course;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top