检查结束日期可能为空的日期重叠
Check for Date overlapping where end date might be null
我目前正在尝试检查结束日期 可能 是否为空的日期重叠。使用我当前的代码,如果场景 1 发生,我可以显示冲突消息。但是,我无法显示方案 2 的冲突消息。例如,
场景 1:结束日期不为空
1/7/2018 至 1/9/2018
1/6/2018 至 1/9/2018
结果:有冲突
情形 2:结束日期为空
1/7/2018 为空
1/6/2018 至 1/9/2018
结果:有冲突
这是我的代码:
if ((A.StartDate < B.EndDate) &&
(B.StartDate < A.EndDate)) {
Console.WriteLine("Conflict");
}
假设 EndDate
为空本质上是 "no end date",因此任何日期总是在此之前。
您可以使用 null 对象模式,将 null 替换为合适的始终匹配的实例(最简单的方法是使用 null-coalescing operator)。
var ed = A.EndDate ?? DateTime.MaxValue;
if (theDate < ed) {
// We're in range
}
我目前正在尝试检查结束日期 可能 是否为空的日期重叠。使用我当前的代码,如果场景 1 发生,我可以显示冲突消息。但是,我无法显示方案 2 的冲突消息。例如,
场景 1:结束日期不为空
1/7/2018 至 1/9/2018
1/6/2018 至 1/9/2018
结果:有冲突
情形 2:结束日期为空
1/7/2018 为空
1/6/2018 至 1/9/2018
结果:有冲突
这是我的代码:
if ((A.StartDate < B.EndDate) &&
(B.StartDate < A.EndDate)) {
Console.WriteLine("Conflict");
}
假设 EndDate
为空本质上是 "no end date",因此任何日期总是在此之前。
您可以使用 null 对象模式,将 null 替换为合适的始终匹配的实例(最简单的方法是使用 null-coalescing operator)。
var ed = A.EndDate ?? DateTime.MaxValue;
if (theDate < ed) {
// We're in range
}