我们有一个名为ABDate
的课程,其中包含Month
,nullable Year
和Day
。如果年份为ABDate
,则创建null
的实例时,日期将被视为重复出现。还有一个IsRecurring
属性。
现在我们有一个IList<ABDate>
,我们会得到一个开始和结束DateTime
。我们如何检查列表中的ABDate
是否位于开始和结束间隔中。
答案 0 :(得分:2)
对于非经常性案例,解决方案是显而易见的。给定集合abDates
以及start
和end
个实例:
var anyInBetween = abDates
.Select(ab => new DateTime(ab.Year.Value, ab.Month, ab.Day))
.Any(d => d >= start && d <= end);
对于重复出现的情况,我们可以观察到,由于没有月份超过31天,因此f(month, day) = month * 100 + day
等函数会将每个不同的(月,日)元组投影到一个唯一的整数中。比较两个这样的整数允许我们确定哪一个源元组在一年中的某一天代表:
// Note: m * 100 is "random". Multiplying by any factor > 31 will work as well.
Func<int, int, int> customTotalOrder = (m, d) => m * 100 + d;
var startIndex = customTotalOrder(start.Month, start.Day);
var endIndex = customTotalOrder(end.Month, end.Day);
var spanningYears = end.Year - start.Year;
var anyInBetween = false;
switch (spanningYears)
{
default:
case 2:
// start => end contains at least one whole year, so obviously...
anyInBetween = abDates.Any();
case 1:
var projection = abDates.Select(ab => customTotalOrder(ab.Month, ab.Day));
anyInBetween = startIndex < endIndex
? projection.Any(i => i >= startIndex && i <= endIndex)
: projection.Any(i => i >= startIndex || i <= endIndex);
case 0:
anyInBetween = abDates
.Select(ab => customTotalOrder(ab.Month, ab.Day))
.Any(i => i >= startIndex && i <= endIndex);
}
答案 1 :(得分:1)
foreach (ABDate a in AbDateList)
{
for ( in checkYear = startDate.Year ;checkYear <= EndDate.Year; checkYear ++)
{
if ((new DateTime(checkYear ,a.Month,a.Day) >= startDate) && (new DateTime(checkYear ,a.Month,a.Day) <= endDate))
{
// I AM IN BETWEEN
}
}
}
}