How to select last hour and groupby minute?

Viewed 49

I want to select data from SQL database using EF, select the last hour and then group by minute.

var result = _dbContext.views
             .Where(x => x.Id == id && x.Created  > DateTime.UtcNow.AddHours(-1))
             .OrderBy(x => x.Created)
             .GroupBy(x=> x.Created.Where({Range is 1 minute}));
1 Answers

DateTime instance has a Minute property which is an int so you can use the x.Created.Minute for grouping:

var result = _dbContext.views
             .Where(x => x.Id == id && x.Created  > DateTime.UtcNow.AddHours(-1))
             .OrderBy(x => x.Created)
             .GroupBy(x=> x.Created.Minute}));
Related