LINQ 中 DateTime.Add(TimeSpan) 的使用问题

.Net技术 码拜 4年前 (2015-12-18) 811次浏览 0个评论

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或其他相似方式),然后在内存中执行逻辑。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明LINQ 中 DateTime.Add(TimeSpan) 的使用问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!