Ash Machine
Ash Machine

Reputation: 9901

Using Linq to select from a List in a List with Contains

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?

Upvotes: 1

Views: 308

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

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

Upvotes: 5

Related Questions