【发布时间】:2021-12-31 17:40:41
【问题描述】:
在我的 Web 应用程序中,我只想显示 2 天前从“DateTime.Today”记录的数据。
获取我尝试过的 2 天前的日期
DateTime twoDaysAgo = DateTime.Today.AddDays(-2);
然后在查询中
smcNews = (from n in db.NewsShare
join e in db.CreateEmployee on n.Created_By equals e.Id join d in db.CreateDepartment on e.DepId equals d.Id
where n.CreatedDate > twoDaysAgo && n.CreatedDate <= DateTime.Today
select new NewsShareViewModel {
Id = n.Id,
UserName = e.EmpName,
Department = d.Department,
Message = n.Comment,
UserId = n.Created_By,
CreatedDate = n.CreatedDate.ToString()
}).ToList();
它不会返回数据。我检查了twoDaysAgo 的值,它就像{12/29/2021 12:00:00 AM}
CreatedDate 中的数据是2021-12-31 13:43:19.957
所以我有什么方法可以通过从日期中删除时间或其他什么来正确查询?
【问题讨论】:
-
n.CreatedDate.Date不起作用吗? -
@Llama 不。当它添加时,我得到了这个错误
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. -
你在使用实体框架吗?
-
@phuzi 是的。代码在控制器内
-
如果您使用的是 LINQ to SQL 或 EF,请记得提及 :) 如果您只是使用代码查询
List<something>等,肯定不会出现错误。
标签: c# entity-framework