我需要检索以下查询的最近5年数据。您能告诉我该怎么办?
var propertyIdList = (from property in _context.Properties
.Where(p => p.CreationTime >= condition for 5 years data restriction)
select new PropertyIdListDto
{
Id = property.Id,
}).OrderBy(p => p.Id).AsNoTracking().ToList();
我知道如何使用TSQL。但我需要使用Linq。
TSQL(样本):
SELECT *
FROM products
WHERE date_column >= add_months( sysdate, -12*5 )
答案 0 :(得分:1)
尝试这样的事情,
var fiveYearsBackDate = DateTime.Now.AddYears(-5);
var propertyIdList = (from property in _context.Properties
.Where(p => p.CreationTime >= fiveYearsBackDate)
select new PropertyIdListDto
{
Id = property.Id,
}).OrderBy(p => p.Id).AsNoTracking().ToList();