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

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

问题描述

我正在尝试运行以下代码,但是比较失败了,没有把我的预期的实体交给我。

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

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

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();

仅如何比较日期?

如果在.StartTime.Value部分使用.Date属性,我会收到一个例外:

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


指定的类型成员'Date'在LINQ to Entities中不支持。
只支持初始化器,实体成员和实体导航属性

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


推荐答案

您可以使用各个成员:

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();

...或使用任何 L2E中支持的其他方法/属性

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

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