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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -