从给定时间段检索数据
问题描述:
我正在尝试获取在过去两周内创建的项目列表。我能够检索14个项目,但我需要14天的项目。我的代码如下所示。我是这种语言的新手,我想我已经修好了,但仍然有问题。任何帮助将apperciated从给定时间段检索数据
public ActionResult SolaceHistory()
{
var model = new SolaceHistoryList();
model.Statuses = OnlineGivingContext.log_SolaceStatus.OrderByDescending(s => s.DateCreated).Take(14).ToList();
return View(model);
}
答
使用以下功能
public ActionResult SolaceHistory()
{
var Criteria=DateTime.Now.AddDays(-14);
var model = new SolaceHistoryList();
model.Statuses = OnlineGivingContext.log_SolaceStatus.OrderByDescending(s => s.DateCreated.Date >= Criteria.Date).ToList();
return View(model);
}
答
{
var Criteria = DateTime.Today.AddDays(-14);
var model = new SolaceHistoryList();
model.Statuses = OnlineGivingContext.log_SolaceStatus
.Where(st => st.DateCreated >= Criteria).
OrderByDescending(s => s.DateCreated).ToList();
return View(model);
}
能够解决我的问题
提示:使用'Where'进行过滤。考虑你真正试图过滤的内容。更大提示:考虑你如何构建一个“截止”日期,以便你能找到东西的地方's.DateCreated> = cutoffDate' ... –
'。凡(X => x.DateCreated> =开始)''哪里开始'大概是'DateTime.UtcNow.AddDays(-14)'? –
[日期之间的LINQ查询]的可能的复制(https://*.com/questions/30662009/linq-query-between-dates) – Liam