以下会引发错误:
public FieldViewer GetFieldViewByFieldIDIPAndUserByDate( int fieldID, string ip, string userID, DateTime date )
{
return this.context.FieldViewers.Where( x =>
x.Field.FieldID == fieldID &&
x.Viewer.IPAddress == ip &&
x.Viewer.User.Id == userID &&
date.Subtract( x.Viewer.CreatedAt ).TotalMinutes >= 10 ).FirstOrDefault();
}
LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression.
我该如何解决这个问题,因为我需要按查询减去。
最佳答案
解决此问题的一种方法是使用 LINQ to Objects 提供程序在 LINQ to Entities 提供程序之外执行过滤部分。为此,请在该操作之前附加对 AsEnumerable()
的调用:
public FieldViewer GetFieldViewByFieldIDIPAndUserByDate( int fieldID, string ip, string userID, DateTime date )
{
return this.context.FieldViewers.Where( x =>
x.Field.FieldID == fieldID &&
x.Viewer.IPAddress == ip &&
x.Viewer.User.Id == userID)
.AsEnumerable()
.Where(x => date.Subtract( x.Viewer.CreatedAt ).TotalMinutes >= 10)
.FirstOrDefault();
}
另一种方法是使用 specialized LINQ to Entities operations 之一, 比如 DiffMinutes
.
关于c# - LINQ to Entities 无法识别方法 'System.TimeSpan Subtract(System.DateTime),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31563153/