Question has been edited. See comments. Changes are in italic.
I have the following list of ordered (start/stop) events that are generated by multiple physical device identified by an ID. For simplicity, only data for one device is shown.
Date Hour ID Event IsStart
18/10/2021 10:35:22 1 DeviceConnected True
18/10/2021 10:20:10 1 DeviceConnected True
18/10/2021 10:12:20 1 DeviceConnected False
18/10/2021 10:12:19 1 DeviceConnected False
18/10/2021 08:24:14 1 DeviceConnected True
Over a given period of time, which is usually 24h, I can't have two or more consecutive starts or stops. I need to remove "duplicates".
In the example above, that would mean, after applying a filter using linq:
Date Hour ID Event IsStart
18/10/2021 10:20:10 1 DeviceConnected True
18/10/2021 10:12:19 1 DeviceConnected False
18/10/2021 08:24:14 1 DeviceConnected True
A start should be followed by a stop or nothing, and the reverse.
This is a typical Gaps-and-Islands problem, right? Let's add the Island number to the initial data set. Unless I'm wrong we should end with something like:
Date Hour ID Event IsStart Island
18/10/2021 10:35:22 1 DeviceConnected True 3
18/10/2021 10:20:10 1 DeviceConnected True 3
18/10/2021 10:12:20 1 DeviceConnected False 2
18/10/2021 10:12:19 1 DeviceConnected False 2
18/10/2021 08:24:14 1 DeviceConnected True 1
Can I do that with Linq? If so, I should be able to retain only the first record on the island.