我对Date的过程有疑问,
CreatedOn = DateTime.Now, I'm writing.
我想列出今天的数据,但我不能这样做。
数据库:2017-12-19 14:46:00.337
var notificationsList = _db.AllNotifications.Where (m => m.CreatedOn == DateTime.Today.Date) .ToList ();
答案 0 :(得分:1)
今天随时查找所有项目的最简单方法是找到今天午夜或之后发生的项目以及明天之前的:
var tomorrow = DateTime.Today.AddDays(1);
...
_db.AllNotifications.Where (m => m.CreatedOn >= DateTime.Today &&
m.CreatedOn < tomorrow)
答案 1 :(得分:0)
如果要列出今天创建的所有项目,您应该忽略实际的时间。例如:
var notificationsList = _db.AllNotifications.Where (m => m.CreatedOn.Date == DateTime.Today.Date) .ToList ();
或者
var notificationsList = _db.AllNotifications.Where (m => DbFunctions.TruncateTime(m.CreatedOn).Value.CompareTo(DbFunctions.TruncateTime(DateTime.Today)) == 0) .ToList ();