Thomas
Thomas

Reputation: 33

How to select last hour and groupby minute?

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

Upvotes: 3

Views: 52

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

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

Upvotes: 3

Related Questions