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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With