I have a list that looks like this. I need to merge this list elements if the dates are 1 days apart ("2022, 01, 01" and "2022, 01, 02), their rate, roomTypeId and Accessibility is exactly the same. The DateFrom and DateTo are ALWAYS the same in the list I am given, every Availability represents one day.
new Availability
{
Accessibility = 1,
RoomTypeId = "12345",
Rate = 50,
DateFrom = new DateTime(2022, 01, 01),
DateTo = new DateTime(2022, 01, 01)
},
new Availability
{
Accessibility = 1,
RoomTypeId = "12345",
Rate = 50,
DateFrom = new DateTime(2022, 01, 02),
DateTo = new DateTime(2022, 01, 02)
},
new Availability
{
Accessibility = 1,
RoomTypeId = "12345",
Rate = 100,
DateFrom = new DateTime(2022, 01, 04),
DateTo = new DateTime(2022, 01, 04)
},
new Availability
{
Accessibility = 6,
RoomTypeId = "12345",
Rate = 100,
DateFrom = new DateTime(2022, 01, 05),
DateTo = new DateTime(2022, 01, 05)
},
new Availability
{
Accessibility = 6,
RoomTypeId = "12345",
Rate = 100,
DateFrom = new DateTime(2022, 01, 6),
DateTo = new DateTime(2022, 01, 06)
},
The result should look like this:
new Availability
{
Accessibility = 1,
RoomTypeId = "12345",
Rate = 50,
DateFrom = new DateTime(2022, 01, 01),
DateTo = new DateTime(2022, 01, 02)
},
new Availability
{
Accessibility = 1,
RoomTypeId = "12345",
Rate = 100,
DateFrom = new DateTime(2022, 01, 04), <- missing (2002, 01, 03) so it should crate a new group
DateTo = new DateTime(2022, 01, 04)
},
new Availability
{
Accessibility = 6, <- different
RoomTypeId = "12345",
Rate = 100,
DateFrom = new DateTime(2022, 01, 05),
DateTo = new DateTime(2022, 01, 06)
}
How can I do this in the most optimal way? I tried grouping the list by RoomTypeId, sorting by date and comparing dates to previous elements, but it's not that yet.