Linq中的DateTime.Compare [英] DateTime.Compare in Linq

查看:49
本文介绍了Linq中的DateTime.Compare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Linq中使用DateTime.Compare:

I am trying to use DateTime.Compare in Linq :

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  

链接建议我们应该使用EntityFunctions修复Linq中的dateTime操作.但是在这里我需要比较完整的日期.甚至也无济于事我.

This link suggests that we should use EntityFunctions to fix dateTime manipulation in Linq. But here I need to compare the complete date. Even this won't help me.

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

The date is in format yyyy-MM-dd.

推荐答案

您不需要DateTime.Compare只需编写ktab.KTABTOM <= DateTime.Now

日期时间可为空的示例:

不编译

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]

这篇关于Linq中的DateTime.Compare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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