5

I'm trying to work out how to group each item by the day it occurred within a time range in Linq to SQL, but I cannot get it working.

   var graph = from o in DB.Context.Occurances
            where o.Occurred > fromDate && o.Occurred <= toDate
            group o by o.Occurred.Date into g
            orderby g.Key ascending 
            select new
                       {
                           Date = g.Key,
                           Occurances = g.Count()
                       };

Every time I run this I get the error

"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

Searching I found this question Here that addresses this issue... but when running this using Entity Framework, I get the exception

"The function is not recognized by SQL Server Compact. [ Name of function = TRUNCATETIME,Data type (if known) = ]"

Any ideas would be greatly appreciated. Thanks!

Community
  • 1
  • 1
anthonyvscode
  • 995
  • 8
  • 15

2 Answers2

1

Use DbFunctions.TruncateTime(o.Occurred) to get Date in LINQ to SQL.

Dmitry Bogatykh
  • 485
  • 5
  • 10
1

The only solution I know of that will definitely work is to manually select a concatenation of the Year, Month, Day properties using String.Concat or integer arithmetic and group on that.

Source: CLR Method to Canonical Function Mapping

Jon
  • 428,835
  • 81
  • 738
  • 806