从给定时间段检索数据

问题描述:

我正在尝试获取在过去两周内创建的项目列表。我能够检索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); 
    } 
+2

提示:使用'Where'进行过滤。考虑你真正试图过滤的内容。更大提示:考虑你如何构建一个“截止”日期,以便你能找到东西的地方's.DateCreated> = cutoffDate' ... –

+2

'。凡(X => x.DateCreated> =开始)''哪里开始'大概是'DateTime.UtcNow.AddDays(-14)'? –

+3

[日期之间的LINQ查询]的可能的复制(https://*.com/questions/30662009/linq-query-between-dates) – Liam

使用以下功能

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); 
    } 
+0

我试图用较早,但它给了我aSystem.NotSupportedException 消息=指定类型的成员“日期”在LINQ是不支持的实体。仅支持初始化程序,实体成员和实体导航属性。 源= 堆栈跟踪: – Elias0852

+0

您是否使用syetem.linq命名空间? – NIts577

 { 
     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); 
    } 

能够解决我的问题