title: Entity Framework Group By with Max Date and count
body: <p>I have the following SQL</p>
  
  <pre><code>SELECT  Tag , COUNT(*) , MAX(CreatedDate)
  FROM    dbo.tblTags
  GROUP BY Tag
  </code></pre>
  
  <p>Which outputs the following:</p>
  
  <pre><code>+-----------------+------------------+-------------------------+
  |       Tag       | (No column name) |    (No column name)     |
  +-----------------+------------------+-------------------------+
  | a great tag     |                1 | 2015-04-01 18:30:31.623 |
  | not a test      |                1 | 2015-04-01 17:46:09.360 |
  | test            |                5 | 2015-04-01 18:13:17.920 |
  | test2           |                1 | 2013-03-07 16:53:54.217 |
  +-----------------+------------------+-------------------------+
  </code></pre>
  
  <p>I'm trying to replicate the output of that query using EntityFramework.</p>
  
  <p>I have the following logic which works:</p>
  
  <pre><code>    var GroupedTags = Tags.GroupBy(c =&gt; c.Tag)
          .Select(g =&gt; new 
          { 
              name = g.Key, 
              count = g.Count(), 
              date = g.OrderByDescending(gt =&gt; gt.CreatedDate).FirstOrDefault().CreatedDate 
          })
          .OrderBy(c =&gt; c.name);
  </code></pre>
  
  <p>But takes horribly long to execute compared to the raw SQL query. Any suggestions on how to optimise my approach? It somehow feels wrong.</p>