Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

c# - Simple way to compare Dates in DateTime attribute using Entity Framework 4 and Linq queries

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...