c# - How to group by DateTime.Date in EntityFramework -


i have device model:

public class devicemodel {     public datetime added { get;set; } } 

and want select devices count, grouped added date (not date , time, date). current implementation not valid, because linq can't translate datetime.date sql:

var result = (     device in devicesrepository.getall()     group device new { date = device.added.date } g     select new         {             date = g.key.date,             count = g.count()         }     ).orderby(nda => nda.date); 

how change query make work?

well, according this msdn document, date property supported linq sql , i'd assume entity framework supports well.

anyway, try query (notice i'm using truncatetime method in order avoid resolving date repeatedly):

var result = device in                  (                      d in devicesrepository.getall()                      select new                       {                           device = d,                           addeddate = entityfunctions.truncatetime(d.added)                       }                  )              orderby device.addeddate              group device device.addeddate g              select new              {                  date = g.key,                  count = g.count()              }; 

hope helps.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -