【问题标题】:Where clause causing cannot compare elements errorWhere 子句导致无法比较元素错误
【发布时间】:2016-06-11 09:11:15
【问题描述】:

我有两个表,Doctors 和 DoctorShifts,两个表之间的关系是一个(Doctor)到多个(DoctorShifts)。

在 DoctorShifts 中,我保存了每位正在工作的医生的星期几。

我需要的是显示今天工作的医生列表,我尝试了以下表达式但它不起作用:

IQueryable<Doctor> TodayDoctorsQuery = _context.Doctors.Where(s => s.DoctorShifts.Where(s2 => s2.DayOfTheWeek == _todayNumber) != null);

错误是:

无法比较“System.Collections.Generic.IEnumerable`1[[MedCare_Software.EDM.DoctorShift, MedCare-Software, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]”类型的元素。仅支持原始类型、枚举类型和实体类型。"}

如何获取今天工作的医生名单?

【问题讨论】:

  • 这与 WPF 有什么关系?
  • 在末尾添加:ToList();

标签: c# lambda


【解决方案1】:

内部 where 子句返回一个 IEnumerable&lt;DoctorShift&gt;,它不能与 null 进行比较。

无论如何,你最好使用Any,如:

var TodayDoctorsQuery = _context.Doctors.Where(
    s=> s.DoctorShifts.Any(s2 => s2.DayOfTheWeek == _todayNumber));

并确保DayOfTheWeek_todayNumber 是同一类型。

【讨论】:

  • 非常感谢,感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多