且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linq异常:只能从LINQ到Entities调用此函数

更新时间:2023-09-22 17:00:40

出现此错误的原因是查询是在内存中执行的,而不是在RDBMS中执行. DiffMilliseconds函数是一个标记,Entity Framework提供程序将其转换为RDBMS特定的SQL以发送到RDBMS.应用于内存中的IQueryable<T>时,该函数不计算其结果,而是引发异常.

The reason you are getting this error is that the query is executed in memory, not in RDBMS. The DiffMilliseconds function is a marker that Entity Framework provider converts to RDBMS-specific SQL to send to your RDBMS. The function does not compute its result when applied to an IQueryable<T> in memory, throwing an exception instead.

如果要在内存中运行此查询,请替换

If you want to run this query in memory, replace

TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart, r.TimeEnd)

使用

TotalTime = (r.TimeEnd - r.TimeStart).TotalMilliseconds

两个日期相减会产生一个 TimeSpan 值从中可以获取其 TotalMilliseconds 属性.

Subtraction of two dates produces a TimeSpan value from which you can take its TotalMilliseconds property.