【问题标题】:Using Linq to select from a List in a List with Contains使用 Linq 从包含列表中的列表中进行选择
【发布时间】:2012-01-12 22:02:00
【问题描述】:

我遇到了语法问题。

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 中的任何学生的所有课程的过滤列表?

【问题讨论】:

    标签: linq select lambda any


    【解决方案1】:
    var result = from course in allCourses
                 where course.Students.Any(x => studentIds.Contains(x.StudentId))
                 select course;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2013-01-15
      相关资源
      最近更新 更多