user649802
user649802

Reputation: 3253

How to retrieve records based on Yesterday date in c#?

Date_Of_Event.Date <= DateTime.Today.Date.AddDays(-1))

Upvotes: 1

Views: 804

Answers (2)

John Gietzen
John Gietzen

Reputation: 49544

You have the comparison backwards

// This should work in LINQ-to-SQL / EntityFramework
Date_Of_Event >= DateTime.Today.AddDays(-1)
&&
Date_Of_Event < DateTime.Today

or

// This will work in LINQ-to-Objects or anywhere else, really.
Date_Of_Event.Date == DateTime.Today.AddDays(-1)

As a side note, it may be a good idea to store the value of DateTime.Today in a variable, so that weird bugs don't show up around midnight (or 11:00pm or 1:00am depending on daylight saving time).

Upvotes: 5

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

Date_Of_Event.Date == DateTime.Today.AddDays(-1)

This will remove the time part of the date time, and keep only the date so you can check for equality instead of the time being inside the correct interval.

Upvotes: 3

Related Questions