开发者

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

开发者 https://www.devze.com 2023-02-15 17:55 出处:网络
Date_Of_Event.Date <= DateTime.Today.Date.AddDays(开发者_开发技巧-1))You have the comparison backwards

Date_Of_Event.Date <= DateTime.Today.Date.AddDays(开发者_开发技巧-1))


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消