C# DateTime。在Linq中进行比较

C# DateTime。在Linq中进行比较,c#,linq,C#,Linq,我正在尝试使用DateTime。请在Linq中进行比较: from --- where DateTime.Compare(Convert.ToDateTime(ktab.KTABTOM), DateTime.Now) < 1 select new { ------- } link建议我们应该使用EntityFunctions来修复Linq中的日期时间操作。但这里我需要比较完整的日期。甚至都帮不了我 日期的格式为yyyy-MM-dd。您不需要日期时间。比较只需写入ktab.KTABT

我正在尝试使用DateTime。请在Linq中进行比较:

from ---  
where DateTime.Compare(Convert.ToDateTime(ktab.KTABTOM), DateTime.Now) < 1
select new 
{
-------
}
link建议我们应该使用EntityFunctions来修复Linq中的日期时间操作。但这里我需要比较完整的日期。甚至都帮不了我


日期的格式为yyyy-MM-dd。

您不需要
日期时间。比较
只需写入
ktab.KTABTOM为什么要将日期信息作为字符串存储在数据库中?你不这样做的原因是为了避免这样的问题。@ScottChamberlain:KTABTOM是可空的DateTime。而且我对数据库没有控制权。我会向关心此事的人提出建议。但是您现在能提供解决方案吗?如果
ktab.KTABTOM
是一个可为空的日期时间,为什么要调用
Convert.ToDateTime
而不直接比较它?您还希望结果中包含NULL吗?@Guru he正在尝试获取小于或等于“now”的日期。@Guru相同的差异:)请记住,在.NET中,直接比较DateTime将忽略时区,只计算刻度,因此如果DateTime不在同一时区,则比较将是错误的。
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  
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]
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]