title: Entity Framework. SQL Group By to EF Group By
body: <p>I am trying to define a standard way to group by year, by month, by day, by hour, etc ...</p>
  
  <p>By reading some SQL documentation it seems the most efficient way would be to use:</p>
  
  <pre><code>GROUP BY dateadd(month, datediff(month, 0, CreatedDate), 0)
  </code></pre>
  
  <p>Note the "month", "year", ...</p>
  
  <p>I then tried to replicate this using:</p>
  
  <pre><code>.GroupBy(x =&gt; SqlFunctions.DateAdd("month", SqlFunctions.DateDiff("month", 0, x.Created), 0))
  
  .GroupBy(x =&gt; EntityFunctions.AddMonths(EntityFunctions.DiffMonths(0, x.Created)))
  </code></pre>
  
  <p>However, both expression failed to compile ...</p>
  
  <p>And I am not sure if this is the way to do it?</p>
  
  <p>How can I correctly replicate that SQL code line in EF?</p>