In my .NET 4.5.2 C# console app, I have a List<SomeItem>
called someItems
with SomeItem
being:
public class SomeItem {
public int A { get; set; }
public int B { get; set; }
public DateTime C { get; set; }
}
I then group these items into a new list based on A
and B
:
var groupedItems = someItems.GroupBy(x => new { x.A, x.B });
I am now looking to introduce C
into the GroupBy
, but with a catch: I would like to group together SomeItems
whose C
property is within +- (plus or minus) of 2 seconds from each other (essentially a 2-second offset). So, for example, two SomeItem
, one having C
set to
2016-03-28 17:58:01.000
and the other to 2016-03-28 17:58:03.000
, would be grouped together. Is that possible?
Edit: For all intents and purposes let's assume there won't be items in the list which could cause 'overlapping' groups (like the question by @J. Steen in the comments)
Edit 2: An example
Assuming the following data:
- 15/06/2017 00:00:02
- 15/06/2017 00:00:04
- 15/06/2017 00:00:06
- 15/06/2017 00:00:09
- 15/06/2017 00:00:11
- 15/06/2017 00:00:15
..and an offset of 2
seconds, I would expect them to be grouped in the following manner:
Group 1:
- 15/06/2017 00:00:02
- 15/06/2017 00:00:04
- 15/06/2017 00:00:06
Group 2:
- 15/06/2017 00:00:09
- 15/06/2017 00:00:11
Group 3:
- 15/06/2017 00:00:15