我已经做到了这一点:
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)
关于c# - Linq 选择昨天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655244/