问题描述
|
我已经做到了:
DateTime yesterday = DateTime.Today.AddDays(-1);
YesterdaysRegistrations = db.tblForumAuthors.Where(c => c.Join_date == yesterday).Count();
肯定有具有昨天加入日期的记录,但这始终返回0!谁能告诉我我做得正确吗?
解决方法
AddDays
保留小时/分钟/秒分量。您要么必须使用(如果c.Join_date仅是日期组件):
DateTime yesterday = DateTime.Today.AddDays(-1).Date;
否则,您比较范围:
DateTime yesterday = DateTime.Today.AddDays(-1).Date;
DateTime yesterdayEnd = DateTime.Today.Date.AddSeconds(-1);
db.tblForumAuthors.Where(c => c.Join_date >= yesterday && c.Join_date < yesterdayEnd)
,您不必浪费时间,只需确保您不进行精确匹配即可。
尝试以下方法:
DateTime today = DateTime.Today; // read once,avoid \"odd\" errors once in a blue moon
DateTime yesterday = today.AddDays(-1);
YesterdaysRegistrations = db.tblForumAuthors.Where(
c => c.Join_date >= yesterday
&& c.Join_date < today).Count();