2016-06-15 68 views
1

有没有什么办法让仅从DateTime.NowDate那也将是类型的DateTime。因为在我的数据库有CreatedDate属性,它是DateTime类型。我如何在LINQ比较日期(同样像SQL查询)

var cmd = _ut.GetRepoInstance<Feedback>().GetAllRecords().Where(x => x.IsStatus == true & x.CreatedDate== DateTime.Now).Count(); 

有人建议可以Linq查询将在DateTime.Now从C#和DateTime类型之间的比较仅日期部分从SQL

+0

'DateTime.Now.Date'给出了'Date'部分权? – Chaitanya

+0

我假设你通过linqtosql查询数据库,并且我已经使用了DbFunctions。 var cmd = _ut.GetRepoInstance ().GetAllRecords()。其中​​(x => x.IsStatus == true&DbFunctions.TruncateTime(x.CreatedDate)== DbFunctions.TruncateTime(DateTime.Now))。Count(); – Rangesh

回答

3

试试这个:

var cmd = _ut.GetRepoInstance<Feedback>().GetAllRecords().Where(x => x.IsStatus == true & x.CreatedDate.Date == DateTime.Now.Date).Count(); 
+0

它会给像'06/15/2016 12:00:00 AM''价值,但我想比较'DateTime.Now.Date'的'06/15/2016'到'06/15/2016 04:30: 00,0''(仅'06/15/2016')的'DateTime'在SQL中 – Steve

+0

谢谢mate..please忽略我的评论:) – Steve

0

DateDateTime.Now财产给你Date值和时间部分将是00:00:00,这是偶数的DateTime数据类型。

所以您的查询是

var cmd = _ut.GetRepoInstance<Feedback>().GetAllRecords().Where(x => x.IsStatus == true & x.CreatedDate.Date == DateTime.Now.Date).Count(); 
+0

谢谢你的输入:) – Steve