6

我正在尝试获取保存在缓存中的数据。但它会在“选择新的 FilterSsrsLog”行中引发异常。例外:只能从 LINQ to Entities 调用此函数

List<ExecutionLog3> reportServerDB = UpdateCache();
        var reportLog = (from r in reportServerDB
                         orderby r.TimeStart descending
                         where ((model.reportName == null ? true : r.ItemPath.Contains(model.reportName)) &&
                          (model.reportFolder == null ? true : r.ItemPath.Contains(model.reportFolder)) &&
                          (r.TimeStart >= startDateTime) &&
                          (r.TimeStart <= endDateTime)
                         )
                   select new FilterSsrsLog
                   {
                       UserName = r.UserName,
                       ReportName = r.ItemPath,
                       ReportFolder = r.ItemPath,
                       Format = r.Format,
                       Parameters = r.Parameters,
                       TimeStart = r.TimeStart,
                       TimeEnd = r.TimeEnd,
                       TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart, r.TimeEnd)
                   });

如果我删除“select new FilterSsrsLog”代码块并编写“select r”,它就可以工作。但我只需要那些列,所以我能做些什么来解决这个问题?

4

1 回答 1

10

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)

with

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

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

于 2013-08-10T20:49:46.047 回答