You need the first time, then you can use TimeSpan.TotalHours
(cast the double
to int
):
DateTime firstDt = dates.Min();
var hourGroups = dates.GroupBy(dt => (int)(dt - firstDt).TotalHours);
Sample:
var dates = Enumerable.Range(1, 30).Select(i => DateTime.Now.AddMinutes(15 * i)).ToList();
and result:
foreach (var dtGroup in hourGroups)
Console.WriteLine(String.Join(", ", dtGroup));
07.11.2017 10:17:47, 07.11.2017 10:32:47, 07.11.2017 10:47:47, 07.11.2017 11:02:47
07.11.2017 11:17:47, 07.11.2017 11:32:47, 07.11.2017 11:47:47, 07.11.2017 12:02:47
07.11.2017 12:17:47, 07.11.2017 12:32:47, 07.11.2017 12:47:47, 07.11.2017 13:02:47
07.11.2017 13:17:47, 07.11.2017 13:32:47, 07.11.2017 13:47:47, 07.11.2017 14:02:47
07.11.2017 14:17:47, 07.11.2017 14:32:47, 07.11.2017 14:47:47, 07.11.2017 15:02:47
07.11.2017 15:17:47, 07.11.2017 15:32:47, 07.11.2017 15:47:47, 07.11.2017 16:02:47
07.11.2017 16:17:47, 07.11.2017 16:32:47, 07.11.2017 16:47:47, 07.11.2017 17:02:47
07.11.2017 17:17:47, 07.11.2017 17:32:47