How to group by DateTime.Date in EntityFramework

Well, according to this MSDN document, Date property is supported by LINQ to SQL and I’d assume that Entity Framework supports it as well.

Anyway, try this query (notice that I’m using TruncateTime method in order to avoid resolving the date repeatedly):

var result = from device in
                 (
                     from d in DevicesRepository.GetAll()
                     select new 
                     { 
                         Device = d, 
                         AddedDate = EntityFunctions.TruncateTime(d.Added) 
                     }
                 )
             orderby device.AddedDate
             group device by device.AddedDate into g
             select new
             {
                 Date = g.Key,
                 Count = g.Count()
             };

Hope this helps.

Leave a Comment