LINQ 中使用DateTime.Add(TimeSpan)方法执行一个查询,执行后发生如下错误:LINQ to Entities does not recognize the method ‘System.DateTime.Add method(System.TimeSpan)’, and this method cannot be translated into a store expression.
看起来有点复杂,下面是主要代码段:
var results = from b in _context.Bookings where b.ScheduleDate.Add(b.StartTime) >= DateTime.UtcNow select b;
可以尝试的解决方法如下:
Try using the SqlFunctions.DateAdd method in the System.Data.Objects.SqlClient namespace. Documentation here. That will convert into the SQL method DateAdd, documented here. You might also be interested in using DateDiff instead, documented here.
In general, look at SqlFunctions for “common language runtime (CLR) methods that call functions in the database in LINQ to Entities queries.” LINQ to Entities cannot convert any method call into SQL, but the functions in that class will work.
Your other option is to execute the LINQ to Entities query (using ToList or something similar) and then perform the logic in memory.
你的其他选择是可以执行Linq实体查询(使用ToList或其他相似方式),然后在内存中执行逻辑。