我需要帮助才能更改我的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)
}
此方法返回上述所有事件,我想将其更改为仅返回上午8:00之间相同日期的事件。第二天上午8点。
例如:
Event 1
。Event 2
。Event 2
。Event 3
。让我们说它就像一天的开始&amp;每天早上8点结束..
我找不到办法,因为我不熟悉.net中的选项和上下文。
答案 0 :(得分:0)
我认为应该这样做:
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;
}