0

我有一系列具有开始日期和结束日期的术语。我怎样才能找到今天是否在条款之间。代码是

  var termsforcurrentCalendar = UnitOfWork.SchoolTerms.Query().Where(x
  => x.SchoolCalendarId == currentCalendar.SchoolCalendarId).ToArray();

   var noOfTerms = termsforcurrentCalendar.Count();

    for (int i = 0; i < noOfTerms; i++)
   {
      if (DateTime.Today > termsforcurrentCalendar[i].EndDate)
      {
          if (i != noOfTerms)
       {
           if (DateTime.Today < termsforcurrentCalendar[i + 1].StartDate)
        {
           datetoBeChecked = termsforcurrentCalendar[i + 1].StartDate;
        }
     }
    }
   }
4

3 回答 3

1

使用一点 linq niceness 怎么样:

var isTodayInTerm = termsforcurrentCalendar.Any(a => 
    DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

根据评论,要获得下一个术语:

var nextTerm = termsforcurrentCalendar.OrderBy(a => a.StartDate).FirstOrDefault(a =>
    a.StartDate >= DateTime.Today);
于 2013-10-24T09:35:25.737 回答
0
  var isTodayInTerm = termsforcurrentCalendar.Any(a =>DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

    if (!isTodayInTerm)
    {
        var firstAvailableDate = termsforcurrentCalendar.FirstOrDefault(z => z.StartDate > DateTime.Today);
        datetoBeChecked = firstAvailableDate.StartDate;
    }
于 2013-10-24T09:56:25.357 回答
0
var isTodayInTerm = termsforcurrentCalendar.Any(a =>DateTime.Today >= a.StartDate && DateTime.Today <= a.EndDate);

if (!isTodayInTerm)
{
    var firstAvailableDate = termsforcurrentCalendar.OrderBy(a=>a.StartDate).FirstOrDefault(z => z.StartDate > DateTime.Today);
    datetoBeChecked = firstAvailableDate.StartDate;
}
于 2013-10-24T10:07:15.050 回答