Apologies for some of the terminology. This is a difficult problem to explain.

I am trying to determine the actual time in a dataset with records containing duplicate and overlapping date time ranges.

Below is a test dataset of a duplicated and overlapping records.

``````            var records = new List<Record>
{
new Record { Name = "Group1 - A", Start = new DateTime(2024, 1, 1, 9, 0, 0), End = new DateTime(2024, 1, 1, 10, 0, 0) },
new Record { Name = "Group1 - B", Start = new DateTime(2024, 1, 1, 9, 30, 0), End = new DateTime(2024, 1, 1, 11, 0, 0) },
new Record { Name = "Group1 - C", Start = new DateTime(2024, 1, 1, 10, 30, 0), End = new DateTime(2024, 1, 1, 12, 0, 0) },
new Record { Name = "Group2 - D", Start = new DateTime(2024, 1, 1, 13, 0, 0), End = new DateTime(2024, 1, 1, 15, 0, 0) },
new Record { Name = "Group3 - E", Start = new DateTime(2024, 1, 2, 22, 0, 0), End = new DateTime(2024, 1, 2, 23, 0, 0) },
new Record { Name = "Group3 - F", Start = new DateTime(2024, 1, 2, 22, 30, 0), End = new DateTime(2024, 1, 3, 5, 0, 0) }
};
``````

I am expecting the above records to be merged into the below three results. Where the Start is the min Start date time of the grouped records and the End is the max End date time of the group records.

Group 1 – Start: 1/1/2023 9am, End: 1/1/2023 12pm
Group 2 – Start: 1/1/2023 1pm, End: 1/1/2023 3pm
Group 3 – Start: 2/1/2023 10pm, End: 2/1/2023 5am

I have been trying various LINQ queries with a group by but I am not getting anywhere close.

Is it possible to solve this using LINQ?

Before anyone asks the solution is preferably a LINQ query as this is what is passed into a rules engine.