[英]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的作业示例,带有开始和结束日期/时间。
What I want the end result to be is shown below. 我想要的最终结果如下所示。 I am having trouble describing it any way but by example.
除了示例以外,我无法以任何方式对其进行描述。
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.