比较dd-mm-yyyy datetime [英] Compare dd-mm-yyyy datetime

查看:96
本文介绍了比较dd-mm-yyyy datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较dd-mm-yyyyDateTime,而不考虑时间。

I want to compare "dd-mm-yyyy" DateTime's without factoring in the time.

我已经尝试将标准DateTime值与我的数据库DateTime像这样:

I have tried comparing the standard DateTime values with my database DateTime's like this:

C#/ LINQ:

 var startDate = DateTime.Today.AddDays(-10);
 var endDate = DateTime.Today;     
     dateList= (from x in db.MY_DB
         where (x.DATE >= startDate && x.DATE < endDate)   
         select x).ToList();

然而,我的列表从来没有被填充,尽管许多条目满足这个条件,我用SQL中的以下查询:

However, my list never gets populated, even though many entries meet this criteria, which I verified with the following query in SQL:

SQL查询:

Select * from db.my_db where date between '13-JUN-2016' and '23-JUN-2016';


推荐答案

使用 DbFunctions.TruncateTime EntityFunctions.TruncateTime 根据您的实体框架版本:

Use DbFunctions.TruncateTime or EntityFunctions.TruncateTime based on your Entity Framework version which:


当用作LINQ to Entities查询的一部分时,此方法调用
规范的TruncateTime EDM函数返回给定日期,并清除
时间部分。

When used as part of a LINQ to Entities query, this method invokes the canonical TruncateTime EDM function to return the given date with the time portion cleared.



var start = DateTime.Now.Date.AddDays(-10);
dateList = (from x in db.MY_DB
    where ((DbFunctions.TruncateTime(x.DATE) < DateTime.Now.Date) &&
    ((DbFunctions.TruncateTime(x.DATE) >= start)
    select x).ToList();

这篇关于比较dd-mm-yyyy datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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