简单的方法使用实体框架4和LINQ查询比较datetime属性日期 [英] Simple way to compare Dates in DateTime attribute using Entity Framework 4 and Linq queries

查看:152
本文介绍了简单的方法使用实体框架4和LINQ查询比较datetime属性日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行下面的代码位,但比较受不上交我我希望它的实体失败。



它比较 06/09/2011○时00分00秒 06/09 / 2011 12时25分零零秒,后者是我的数据库记录值。所以这就是为什么比较失败,我没有得到我所需要的记录。



我只是想比较,如果日期匹配,我不要。'T关心时间

 今天的DateTime = DateTime.Now.Date; 
VAR newAuctionsResults = repo.FindAllAuctions()
。凡(A => a.IsActive ==真|| a.StartTime.Value ==今日)
.ToList();



我怎么能只比较日期?



如果在.StartTime.Value部分使用.Date财产,我得到一个异常:




指定类型的成员'日期'在LINQ是不支持的实体。
只有初始化,实体成员和实体导航属性
的支持。



解决方案

您可以使用个人会员:

  VAR newAuctionsResults = repo.FindAllAuctions()
。凡( A => a.IsActive ==真
||(a.StartTime.Value.Year == todayYear
和;&安培; a.StartTime.Value.Month == todayMonth
和;&安培; a.StartTime.Value.Day == todayDay))
.ToList();



...或使用任何的 /en-us/library/bb738681.aspx\">the其他方法/属性。


I'm trying to run the following bit of code, but the comparison fails by not handing my the entities I expect it to.

It's comparing 06/09/2011 0:00:00 to 06/09/2011 12:25:00, the latter being my databases record value. So that's why the comparison is failing and I'm not getting the records I need.

I'm just trying to compare if the dates match up, I'm don't care about the time.

DateTime today = DateTime.Now.Date;
var newAuctionsResults = repo.FindAllAuctions()
                        .Where(a => a.IsActive == true || a.StartTime.Value == today)
                        .ToList();

How can I compare only the dates?

If use the .Date property in the .StartTime.Value part, I get an exception:

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

解决方案

You can use the individual members:

var newAuctionsResults = repo.FindAllAuctions()
                        .Where(a => a.IsActive == true 
                                    || (a.StartTime.Value.Year == todayYear
                                        && a.StartTime.Value.Month == todayMonth
                                        && a.StartTime.Value.Day == todayDay))
                        .ToList();

...or use any of the other methods/properties supported in L2E.

这篇关于简单的方法使用实体框架4和LINQ查询比较datetime属性日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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