GroupBy Hour, then on average between two points

I have a database that stores records, and these records have a date and time. As a request, I built a graph to show how many records were recorded for each hour of the day throughout the year.

So, for this, I just grouped by hours and counted the number of objects in each of these 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 use this information to enter a line chart, and here is what it looks like:

enter image description here

.. , . , , . .. 0 35.. 1 41.. , , 35 + 41 = 76/2 = 38.. 38 0 1. 2 . .

, , , .. .

.

UPDATE

, , 30- . :

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();

6000 , .

2

, , 5- , ..

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() };

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; }
}
+4
2

:

lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
    .GroupBy(x => x.RecordDateTime.Hour + (x.RecordDateTime.Minute >= 30 ? 0.5 : 0))
    .Select(x => new object[] { x.Count() }).ToArray();

, 0 29 (: 1:20 1), 30 59 ( : 1:40 1.5 ).

, (: 1:50 > 2, 1:35 > 1.5), :

lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
    .GroupBy(x => x.RecordDateTime.Hour + (Math.Round(x.RecordDateTime.Minute / 30f, MidpointRounding.AwayFromZero) / 2))
    .Select(x => new object[] { x.Count() }).ToArray();
+2

, , , (, 08:30 09:30).

, , - :

var lstByHour =
    lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
        .GroupBy(x => {return x.RecordDateTime.Minutes > 30 ? x.RecordDateTime.Hour + 1 : x.RecordDateTime.Hour})
        .Select(x => new object[] {x.Count()}).ToArray();

EDIT

var lstByHour =
    lstAllRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
        .GroupBy(x => {return x.RecordDateTime.Minutes > 30 ? x.RecordDateTime.Hour + 0.5 : x.RecordDateTime.Hour})
        .Select(x => new object[] {x.Count()}).ToArray();
0

Source: https://habr.com/ru/post/1694536/


All Articles