从给定的时间段检索数据

时间:2017-06-12 16:15:21

标签: c#

我正在尝试获取过去两周内创建的项目列表。我可以检索14项,但我想要14天的项目。我的代码如下所示。我是这种语言的新手,我认为我修复了它但仍有问题。任何帮助都会得到解决

public ActionResult SolaceHistory()
    {

        var model = new SolaceHistoryList();
        model.Statuses = OnlineGivingContext.log_SolaceStatus.OrderByDescending(s => s.DateCreated).Take(14).ToList();
        return View(model);
    }

2 个答案:

答案 0 :(得分:0)

使用以下功能

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

答案 1 :(得分:0)

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

能够解决我的问题