简体   繁体   English

计算多个日期/时间范围之间的交点的位置和数量?

[英]Calculate location and number of intersections between multiple date/time ranges?

I need to calculate the location of intersections between multiple date ranges, and the number of overlapping intersections. 我需要计算多个日期范围之间的相交位置以及重叠的相交数量。 Then I need to show which date/time ranges overlap each of those intersecting sections. 然后,我需要显示哪些日期/时间范围与这些相交的部分重叠。 It is slightly more complicated than that so I'll do my best to explain by providing an example. 它比这稍微复杂一些,因此我将通过提供一个示例来尽力解释。 I am working in VB.Net, but C# examples are acceptable as well as I work in both. 我正在VB.Net中工作,但是C#示例也是可以接受的,并且我在这两个示例中都可以工作。

We have several high risk tasks that involve the same system. 我们有几个涉及同一系统的高风险任务。 Below I have three example jobs named HR1/2/3/4 with start and end date/times. 下面有三个名为HR1 / 2/3/4的作业示例,带有开始和结束日期/时间。

  • HR1 {1/6/2010 10:00 - 1/6/2010 15:00} HR1 {1/6/2010 10:00-1/6/2010 15:00}
  • HR2 {1/6/2010 11:00 - 1/6/2010 18:00} HR2 {1/6/2010 11:00-1/6/2010 18:00}
  • HR3 {1/6/2010 12:00 - 1/6/2010 14:00} HR3 {1/6/2010 12:00-1/6/2010 14:00}
  • HR4 {1/6/2010 18:00 - 1/6/2010 20:00} HR4 {1/6/2010 18:00-1/6/2010 20:00}

What I want the end result to be is shown below. 我想要的最终结果如下所示。 I am having trouble describing it any way but by example. 除了示例以外,我无法以任何方式对其进行描述。

  • HRE1 {1/6/2010 10:00 - 1/6/2010 11:00} - Intersects 1 HRE1 {1/6/2010 10:00-1/6/2010 11:00}-相交1
  • {End Time Split 1, for readability only, not needed in solution} {结束时间拆分1,仅用于可读性,解决方案中不需要}
  • HRE1 {1/6/2010 11:00 - 1/6/2010 12:00} - Intersects 2 HRE1 {1/6/2010 11:00-1/6/2010 12:00}-相交2
  • HRE2 {1/6/2010 11:00 - 1/6/2010 12:00} - Intersects 2 HRE2 {1/6/2010 11:00-1/6/2010 12:00}-相交2
  • {End Time Split 2, for readability only, not needed in solution} {结束时间拆分2,仅用于可读性,解决方案中不需要}
  • HRE1 {1/6/2010 12:00 - 1/6/2010 14:00} - Intersects 3 HRE1 {1/6/2010 12:00-1/6/2010 14:00}-相交3
  • HRE2 {1/6/2010 12:00 - 1/6/2010 14:00} - Intersects 3 HRE2 {1/6/2010 12:00-1/6/2010 14:00}-相交3
  • HRE3 {1/6/2010 12:00 - 1/6/2010 14:00} - Intersects 3 HRE3 {1/6/2010 12:00-1/6/2010 14:00}-相交3
  • {End Time Split 3, for readability only, not needed in solution} {结束时间拆分3,仅出于可读性考虑,解决方案中不需要}
  • HRE1 {1/6/2010 14:00 - 1/6/2010 15:00} - Intersects 2 HRE1 {1/6/2010 14:00-1/6/2010 15:00}-相交2
  • HRE2 {1/6/2010 14:00 - 1/6/2010 15:00} - Intersects 2 HRE2 {1/6/2010 14:00-1/6/2010 15:00}-相交2
  • {End Time Split 4, for readability only, not needed in solution} {结束时间拆分4,仅用于可读性,解决方案中不需要}
  • HRE2 {1/6/2010 15:00 - 1/6/2010 18:00} - Intersects 1 HRE2 {1/6/2010 15:00-1/6/2010 18:00}-相交1
  • {End Time Split 5, for readability only, not needed in solution} {结束时间拆分5,仅用于可读性,解决方案中不需要}
  • HR4 {1/6/2010 18:00 - 1/6/2010 20:00} - Intersects 1 HR4 {1/6/2010 18:00-1/6/2010 20:00}-相交1

Any help would be greatly appreciated. 任何帮助将不胜感激。

var timePoints = (from r in ranges select r.Start)
    .Concat(from r in ranges select r.End)
    .Distinct().OrderBy(dt => dt).ToArray();

var intersections = from i in Enumerable.Range(0, timePoints.Length - 1)
                    let start = timePoints[i]
                    let end = timePoints[i + 1]
                    from range in ranges
                    where range.Start <= start && range.End >= end
                    select new { Range = range, Start = start, End = end };

EDIT: Modified code that counts intersections: 编辑:计数交叉点的修改代码:

var timePoints = (from r in ranges select r.Start)
    .Concat(from r in ranges select r.End)
    .Distinct().OrderBy(dt => dt).ToArray();

var intersectionGroups = from i in Enumerable.Range(0, timePoints.Length - 1)
                         let start = timePoints[i]
                         let end = timePoints[i + 1]
                         select new
                         {
                             Start = start,
                             End = end,
                             Ranges =
                                 from range in ranges
                                 where range.Start <= start && range.End >= end
                                 select range
                         };

var intersections = from intGroup in intersectionGroups
                    let count = intGroup.Ranges.Count()
                    from range in intGroup.Ranges
                    select new
                    {
                        Range = range,
                        Start = intGroup.Start,
                        End = intGroup.End,
                        Count = count
                    };

I don't know what do you want to do with the result, but it may be better to use intersectionGroups rather than intersections . 我不知道您要如何处理结果,但最好使用intersectionGroups而不是intersections

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM