我正尝试在Linq中使用DateTime.Compare:
from ---
where DateTime.Compare(Convert.ToDateTime(ktab.KTABTOM), DateTime.Now) < 1
select new
{
-------
}
但这给了我一个错误:
LINQ to Entities does not recognize the method 'System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)' method, and this method cannot be translated into a store expression
This链接建议我们应该使用EntityFunctions来修复Linq中的dateTime操作。但在这里,我需要比较完整的日期。即使是this也不会帮我。
日期的格式为yyyy-MM-dd。
发布于 2013-11-20 07:57:04
您不需要DateTime.Compare
,只需编写ktab.KTABTOM <= DateTime.Now
即可
具有可空DateTime的示例:
不编译
from p in Projects
where DateTime.Compare(DateTime.Now, p.EndDate) <= 0
select p.EndDate
和
from p in Projects
where DateTime.Now <= p.EndDate
select p.EndDate
翻译为
SELECT
[Extent1].[EndDate] AS [EndDate]
FROM [dbo].[Project] AS [Extent1]
WHERE CAST( SysDateTime() AS datetime2) <= [Extent1].[EndDate]
不带可空DateTime的示例:
from p in Projects
where DateTime.Compare(DateTime.Now, p.StartDate) <= 0
select p.StartDate
和
from p in Projects
where DateTime.Now <= p.StartDate
select p.StartDate
两者都转换为
SELECT
[Extent1].[StartDate] AS [StartDate]
FROM [dbo].[Project] AS [Extent1]
WHERE (SysDateTime()) <= [Extent1].[StartDate]
https://stackoverflow.com/questions/20089594
复制相似问题