




Я предполагаю, что вы говорите о предложении 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();
}
Если вы не хотите, чтобы ваш код бросал
LINQ to Entities does not recognize the method 'System.DateTime AddYears(Int32)' method, and this method cannot be translated into a store expression.
Я бы предложил использовать DbFunctions.AddYears. Кроме того, если вас интересуют только даты, а не время, вы можете использовать DbFunctions.TruncateTime
Что-то вроде этого-
DbFunctions.TruncateTime(x.date) ==
DbFunctions.TruncateTime(DbFunctions.AddYears(DateTime.Today, 1))