使用Linq操纵DateTime

时间:2014-05-29 17:58:02

标签: c# linq

我在数据库中有一个解决方案表,我想显示截至目前不到一天的任何解决方案(DateTime.Now)谢谢

IQueryable<Solution> Solutions=
                        from x in db.Solutions
                        where x.Created_at == DateTime.Now -1
                        select x;

1 个答案:

答案 0 :(得分:3)

使用DateTime.AddDays获取目标日期。然后使用>=比较来过滤比目标日期(或等于它)更旧的解决方案:

IQueryable<Solution> Solutions =
       from s in db.Solutions
       where s.Created_at >= DateTime.Now.AddDays(-1)
       select s;

注意:这将为您提供24小时后的实体。如果您想从昨天开始获取实体,请使用DateTime.Today代替DateTime.Now