I have a database that holds records and those records have a datetime. As a request, I have built a timeline to show how many records were recorded for each hour of the day for the entire year.
So, for that I simply grouped by the hour and counted the number of objects in each of those hours:
var lstByHour =
lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
.GroupBy(x => x.RecordDateTime.Hour)
.Select(x => new object[] {x.Count()}).ToArray();
I am using that information to put into a line chart and here is how that looks:
But now, I have been asked to get the half-hour.. or the point that is in between each hour.. so what I was thinking was to take two points and find the average and that would give the middle number between two points. For example.. on the 0
hour that point is 35.. on the 1
hour that point is 41.. so in order to find the number in between, I would just add 35 + 41 = 76 / 2 = 38.. then plot 38 in between the 0
and 1
hour. In the case of dividing odd numbers by 2 and ending up with a decimal.. I would like to round up.
My problem is that I'm not sure on where to start on editing my lambda expression to get that value.. and continuously get that value between each hour.
Any help is appreciated.
UPDATE
Using the duplicate question I'm confused on how my code would look to get 30 minute intervals. I have this:
var groups = lstAllRecords.GroupBy(x =>
{
var stamp = x.RecordDateTime;
stamp = stamp.AddMinutes(-(stamp.Minute % 30));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return stamp;
})
.Select(g => new object[] {g.Count()})
.ToArray();
This is resulting in over 6000 records and the line chart isn't loading.
UPDATE 2
I've tried both the accepted answer and the second answer since they both have answer for 5 minute intervals and it's not working for me..
var grouped = from s in lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
group s by new DateTime(s.RecordDateTime.Year, s.RecordDateTime.Month,
s.RecordDateTime.Day, s.RecordDateTime.Hour, s.RecordDateTime.Minute / 2, 0) into g
select new object[]{ g.Count() };
Here is my data model
public partial class DailyRecord
{
public int ID { get; set; }
public System.DateTime RecordDateTime { get; set; }
public string IncidentNumber { get; set; }
public string Summary { get; set; }
public bool deleted { get; set; }
}