【发布时间】:2016-03-13 16:09:39
【问题描述】:
对于学校项目,我需要过滤同时注册了多个课程的学生。我不想通过过程/视图查询数据库,而是想使用 LINQ 在内存中对其进行过滤以用于学习目的。
根据调试器,一切似乎都很好,但是我的 linq 查询的结果是 0,我不知道是怎么回事。
代码如下:
foreach (Timeblock tb in ctx.Timeblocks)
{
List<Student> doublestudents = new List<Student>();
//Get the schedules matching the timeblock.
Schedule[] schedules = (from sched in ctx.Schedules
where sched.Timeblock.Id == tb.Id
select sched).ToArray();
/\/\/\Gives me 2 schedules matching that timeblock.
if (schedules.Count() > 1)
{
doublestudents = (from s in ctx.Students
where s.Courses.Contains(schedules[0].Course) && s.Courses.Contains(schedules[1].Course)
select s).ToList();
Console.WriteLine(doublestudents.Count); <<< count results in 0 students.
}
}
在调试时似乎一切正常。
每个学生都有一个列表,每个课程都有一个列表
schedules[0].Course 的 ID 为 1 schedules[0].Course 的 ID 为 6
ID 为 14 的学生的列表中有这两个课程。
linq 查询仍然没有返回这个学生。这可能是因为它不是同一个引用,当然它不会在 .Contains() 中找到匹配项吗?
这让我完全发疯,因为我尝试的每一种方式都不会在匹配时返回任何结果......
【问题讨论】: