我有这个LINQ查询:
var resourcePlanningInWeek = resourcePlanning.Where(rp => rp.PlanDate >= dateFrom && rp.PlanDate <= dateTo);
var holidays = new HolidayManager().GetByPeriod(dateFrom, dateTo);
var resourcePlanningExcludedHolidays= resourcePlanningInWeek.Where(rpiw => ( holidays.Where(h => h.HolidayDate = rpiw.PlanDate).Count = 0))
执行时,我收到以下错误:
无法将类型'DateTime'隐式转换为'bool'
有人知道为什么吗?
答案 0 :(得分:2)
==
代替=
。这与最后一行的两个地方有关。Count
的调用缺少括号。var resourcePlanningExcludedHolidays = resourcePlanningInWeek
.Where(rpiw => holidays.Where(h => h.HolidayDate == rpiw.PlanDate)
.Count() == 0);
但是,有一种更好的方式来写这个:
var resourcePlanningExcludedHolidays = resourcePlanningInWeek
.Where(rpiw => !holidays.Any(h => h.HolidayDate == rpiw.PlanDate));
这样更好,因为:
holidays
。 Count()
始终列举完整列表。更好的方法是使用HashSet<DateTime>
:
var holidays
= new HashSet<DateTime>(new HolidayManager().GetByPeriod(dateFrom, dateTo)
.Select(x => x.HolidayDate));
var resourcePlanningExcludedHolidays
= resourcePlanningInWeek.Where(rpiw => !holidays.Contains(rpiw.PlanDate));
答案 1 :(得分:1)
你错过了一个相同的符号:
h.HolidayDate = rpiw.PlanDate
应该是:
var resourcePlanningExcludedHolidays=
resourcePlanningInWeek.Where(rpiw => ( holidays.Where(h => h.HolidayDate == rpiw.PlanDate).Count = 0))