Here is a test program I ran, you can adapt the code based on your specific requirements. It uses LINQ's GroupBy()
predicate, and is based completely on this answer to a similar question (I changed it to check hours instead of minutes).
public class Temp
{
public DateTime activityTime { get; set; }
public object item_info { get; set; }
}
Of course you can use your own class, mine is just for testing.
List<Temp> items = new List<Temp> //populate with dummy data
{
new Temp { activityTime = DateTime.Parse("2017-12-11 11:43:00"), item_info = "something1" },
new Temp { activityTime = DateTime.Parse("2017-12-11 11:46:00"), item_info = "something2" },
new Temp { activityTime = DateTime.Parse("2017-12-11 11:57:00"), item_info = "something3" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:02:00"), item_info = "something4" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:04:00"), item_info = "something5" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:06:00"), item_info = "something6" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:58:00"), item_info = "something7" },
new Temp { activityTime = DateTime.Parse("2017-12-11 13:05:00"), item_info = "something8" },
new Temp { activityTime = DateTime.Parse("2017-12-11 13:29:00"), item_info = "something9" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:53:00"), item_info = "something10" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:55:00"), item_info = "something11" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:59:00"), item_info = "something12" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:59:00"), item_info = 13 },
new Temp { activityTime = DateTime.Parse("2017-12-11 15:26:00"), item_info = 15 }
};
var groups = items.GroupBy(x =>
{
var stamp = x.activityTime;
stamp = stamp.AddMinutes(-(stamp.Minute));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return stamp;
})
.Select(g => new { TimeGroup = g.Key.ToString("h tt") + " - " + g.Key.AddHours(1).ToString("h tt"), Value = g.Count() })
.ToList();
To change the grouping, you can modify the GroupBy()
predicate.
The TimeGroup
variable can be changed based on what display you need (date formatting, or you can add 1 minute like in your case to have "02:01 - 04:00".
For an interval of 2 hours:
var groups = items.GroupBy(x =>
{
var stamp = x.activityTime;
stamp = stamp.AddHours(-(stamp.Hour % 2));
stamp = stamp.AddMinutes(-(stamp.Minute));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return stamp;
})
.Select(g => new { TimeGroup = g.Key.ToString("h tt") + " - " + g.Key.AddHours(2).ToString("h tt"), Value = g.Count() })
.ToList();
Warning, this operation may be time-heavy for larger datasets.