gpt4 book ai didi

c# - LINQ to Entities 中使用的 AddDays 在除一台服务器外的所有服务器中抛出错误

转载 作者:行者123 更新时间:2023-11-30 20:46:07 27 4
gpt4 key购买 nike

在开发环境中,以下 SQL 语句有效,但在任何其他环境中无效:

System.NotSupportedException: LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.

相关代码:

bool hasSomethinglast14days = (from a in db.someTable
where a.SomeIntColumn == someIntVariable
&& a.CreateDate >= DateTime.UtcNow.AddDays(-14)
select a.someColumn).Any();

最佳答案

您使用的提供商无法将 AddDays() 方法转换为有效的 SQL 语句。

因为你要减去的日期无论如何都不依赖于数据库中的任何值,所以只需先做减法(在查询之外),然后使用结果值:

var pastDate = DateTime.UtcNow.AddDays(-14);

bool hasSomethinglast14days = (from a in db.someTable
where a.SomeIntColumn == someIntVariable
&& a.CreateDate >= pastDate
select a.someColumn).Any();

至于它在您的开发环境中工作,我没有答案。我很惊讶它确实如此。

关于c# - LINQ to Entities 中使用的 AddDays 在除一台服务器外的所有服务器中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com