I have
var q = db.Games.Where(t => t.StartDate >= DateTime.Now).OrderBy(d => d.StartDate);
But it compares including time part of DateTime. I really don’t need it.
How to do it without time?
Thank you!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just use
DateTime.Todayproperty to take the current date. There’s no need to truncatet.StartDatein this case (and doing so may incur performance penalties).Note that I’ve explicitly evaluated
DateTime.Todayonce so that the query is consistent – otherwise each time the query is executed, and even within the execution,Todaycould change, so you’d get inconsistent results. For example, suppose you had data of:Surely either both entries 1 and 3 should be in the results, or neither of them should… but if you evaluate
DateTime.Todayand it changes to March 9th after it’s performed the first two checks, you could end up with entries 1, 2, 4.Of course, using
DateTime.Todayassumes you’re interested in the date in the local time zone. That may not be appropriate, and you should make absolutely sure you know what you mean. You may want to useDateTime.UtcNow.Dateinstead, for example. Unfortunately,DateTimeis a slippery beast…EDIT: You may also want to get rid of the calls to
DateTimestatic properties altogether – they make the code hard to unit test. In Noda Time we have an interface specifically for this purpose (IClock) which we’d expect to be injected appropriately. There’s a "system time" implementation for production and a "stub" implementation for testing, or you can implement it yourself.You can use the same idea without using Noda Time, of course. To unit test this particular piece of code you may want to pass the date in, but you’ll be getting it from somewhere – and injecting a clock means you can test all the code.