从.Net中的SQL db获取对时间敏感的特定数据

从.Net中的SQL db获取对时间敏感的特定数据,.net,sql,linq,api,datetime,.net,Sql,Linq,Api,Datetime,我需要帮助来更改我的API的方法之一。 我有一个DB,几乎没有这样的事件: EventName: Event 1 EventDate: 2013-08-15 00:00:00:00.000 isActive: true EventName: Event 2 EventDate: 2013-08-16 00:00:00:00.000 isActive: true EventName: Event 3 EventDate: 2013-08-17 00:00:00:00.000 isActive:

我需要帮助来更改我的API的方法之一。 我有一个DB,几乎没有这样的事件:

EventName: Event 1 EventDate: 2013-08-15 00:00:00:00.000 isActive: true

EventName: Event 2 EventDate: 2013-08-16 00:00:00:00.000 isActive: true

EventName: Event 3 EventDate: 2013-08-17 00:00:00:00.000 isActive: true
现在,我有了这个方法:

public IQueryable<Event> allActiveAndToday(){
     return this.Where(e => e.IsActive)
}
public IQueryable allActiveAndToday(){
返回这个。其中(e=>e.IsActive)
}
此方法返回上面的所有事件,我希望将其更改为仅返回同一日期内上午8:00到次日上午8:00之间的事件

例如:

  • 如果在2013年8月16日上午7:00调用该方法,结果将是
    事件1
  • 如果在2013年8月16日上午9:00调用该方法,结果将是
    事件2
  • 如果在2013年8月17日上午7:00调用该方法,结果将是
    事件2
  • 如果在2013年8月17日上午9:00调用该方法,结果将是
    事件3
比方说,这就像每天早上8:00开始和结束一天


我找不到这样做的方法,因为我不熟悉.net中的选项和上下文。

我认为应该这样做:

public IQueryable<Event> allActiveAndToday(){
    DateTime currentDt = DateTime.Now;
    DateTime start = new DateTime(currentDt.Year, currentDt.Month, currentDt.Day, 8, 0, 0);
    DateTime end = start.AddDays(1);

    // This will check if the the current date/time falls between 8AM and 8AM (the following day)
    // If not then set the currentDt to yesterday's date.
    if (!(currentDt >= start && currentDt <= end))
    {
        currentDt = currentDt.AddDays(-1);
    }

    // Then do your query here...
    var events = this.Where(x => x.IsActive && x.EventDate.Date == currentDt.Date).ToList();

    return events;
}
public IQueryable allActiveAndToday(){
DateTime currentDt=DateTime.Now;
日期时间开始=新日期时间(当前日期年、当前日期月、当前日期日、8、0、0);
DateTime end=start.AddDays(1);
//这将检查当前日期/时间是否在上午8点到上午8点(次日)之间
//如果没有,则将currentDt设置为昨天的日期。
如果(!(currentDt>=start&¤tDt x.IsActive&&x.EventDate.Date==currentDt.Date).ToList();
返回事件;
}

太好了!检查你的答案并回来标记它!谢谢!这是一个很好的答案,但是你可能会考虑排除结束日期:<代码> CurrutdT>结尾< /Cord>。因为这通常暗示着这些范围。如果这是一个Web应用程序,你还没有解决“现在是谁的问题”?.使用
DateTime.Now
将为您提供服务器的时间。这可能根本不是用户的时间。我认为这是我的问题,因为我没有得到我想要的结果,我将再次检查,然后立即通知您。