地址类型的生效日期和到期日期

Effective Date & Expiration Date of an Address Type

  1. Please be patient while reading the question as I am finding this one little hard to explain.
  2. Kindly suggest a better title so that it helps others in the community.

我有 3 个字段。即。

  1. 地址类型 - 下拉列表(值:'Billing'、'Corporate')
  2. 生效日期 - 日期选择器(mm/dd/yyyy),必填
  3. 到期日期 - 日期选择器(mm/dd/yyyy),非强制性

我需要应用以下验证: 不能同时激活两个相同类型的地址。

示例请参考下方截图。

现在,我有 2 个列表(日期时间)即。 C# 中的 addressesEffectiveDatesList、addressesExpirationDatesList。我不知道如何实现这一目标。

您需要检查日期范围是否重叠。这里有一个很好的答案:Determine Whether Two Date Ranges Overlap

基本上,

(StartA <= EndB) and (EndA >= StartB)

并且您可以将 DateTime 对象与 C# 中的 <= 和 >= 进行比较。

正如 SadiRubaiyet 在评论中提到的,如果您让每个人的地址按开始日期排序,事情会变得更快,因为这意味着您只需要将每个日期范围与下一个日期范围进行比较。

如果没有订购,您必须将每个日期范围与该人的每个 个其他地址进行比较。

我已经为你创建了一个fiddlehere

我建议您也进行客户端验证。 我假设你的日期列表是有序的,所以开始日期和结束日期的第 i 个元素将代表第 i 个间隔,所以

行中的内容
           //Checking to make sure all effective dates have end date, except the last one.
            for (var i = 1; i < effectiveEndDates.Count; i++)
            {
                if (!effectiveEndDates[i - 1].HasValue)
                {
                    result = "cannot have a previous billing address without and end date.";
                }
            }

            //Ensure all the startDates Dates are on or after the next end Date
            //This solution will work if the effective dates are in order.
            for (var i = 1; i < effectiveStartDates.Count; i++)
            {
                var effectiveEndDate = effectiveEndDates[i - 1];
                if (effectiveEndDate != null && effectiveEndDate.Value > effectiveStartDates[i])
                {
                    result = "cannot have 2 active billing address.";
                }
            }

只要日期按顺序(如生效日期)就可以解决问题。希望这有帮助

更新:我已经更新了 fiddle 以确保没有任何内容与无限范围重叠。 [检查这个] (https://dotnetfiddle.net/nmyA3P)