【发布时间】:2021-09-17 03:40:47
【问题描述】:
我现有的 Linq 语句无法在 where 条件中应用可为空的日期时间模型值。当我使用 shortdatestring() 应用日期时间时,会显示以下错误:
.Where(ti => ti.Outer.EmpCode == __empcode_0 && ti.Outer.ClockDateFormatted == __ToShortDateString_1)' 无法翻译。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估
这是代码
考勤模式
public string Code { get; set; }
public DateTime? ClockedDate { get; set; }
public ICollection<Attendance> GetClockList(string code, DateTime datefrom)
{
ICollection<Attendance> data = new List<Attendance>();
data = (from log in ctx.Attendance
join emp in ctx.Employee on log.EmpCode equals emp.EmployeeID.ToString()
where log.EmpCode == empcode
&& log.ClockedDate.Value.ToShortDateString() == datefrom.ToShortDateString() // Here I have to give the ClockedDate without time
select new Attendance
{
EmployeeName = emp.EmployeeName,
ClockedDate = log.ClockedDate,
}
).ToList();
return data;
}
但在 where 条件下使用shortdateString() 给出日期时间时会显示错误
【问题讨论】:
-
您是否尝试过使用
DateTime.Dateproperty?例如:log.ClockedDate.Value.Date == datefrom.Date? -
在比较之前不要转换数据库列,它会影响性能。 Use a date interval.
标签: c# entity-framework linq .net-core entity-framework-core