我想使用LINQ检查给定日期是否比今天的日期提前一个月.
这是什么语法?
提前致谢.
我假设你在where子句中谈论.它与在其他地方比较两个DateTime对象的方式基本相同.
using (DataContext context = new DataContext()) { var query = from t in context.table where t.CreateDate.Date < DateTime.Today.AddMonths(-1) select t; }
只需添加,在LINQ To Entities中,您必须与变量进行比较.例:
DateTime lastMonth = DateTime.Today.AddMonths(-1); using (var db = new MyEntities()) { var query = from s in db.ViewOrTable orderby s.ColName where (s.StartDate > lastMonth) select s; _dsResults = query.ToList(); }