使用DateTime.Add(时间跨度)与LINQ [英] Using DateTime.Add(TimeSpan) with LINQ

查看:129
本文介绍了使用DateTime.Add(时间跨度)与LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行像一个波纹管的查询。它实际上更多的是复杂的,但在这里是重要的部分:

I have to run a query like the one bellow. It is actually more complex, but here is the part that matters:

var results =
    from b in _context.Bookings
    where b.ScheduleDate.Add(b.StartTime) >= DateTime.UtcNow
    select b;



但它提供了以下错误:

But it gives the following error:

LINQ到实体无​​法识别方法System.DateTime.Add
法(System.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.

如何解决此问题?

先谢谢了。

推荐答案

尝试使用 SqlFunctions.DateAdd 方法在 System.Data.Objects.SqlClient 命名空间。文档 rel=\"nofollow\">。这将转换成SQL方法使用DateAdd ,记录这里。您可能也有兴趣使用则DateDiff 相反,记录的这里

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.

在一般情况下,看的 SqlFunctions 了解公共语言运行时(CLR),为实体查询调用数据库函数在LINQ的方法。 LINQ到实体不能任何方法调用转换为SQL,但在类的功能将正常工作。

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.

您另一种选择是执行LINQ到实体查询(使用了ToList 或类似的),然后执行内存中的逻辑的东西。

Your other option is to execute the LINQ to Entities query (using ToList or something similar) and then perform the logic in memory.

这篇关于使用DateTime.Add(时间跨度)与LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆