title: Lambda : GroupBy multiple columns where one of them is DbFunctions.TruncateTime()
body: <p><strong>NOTE</strong> : This is NOT a duplicate of this <a href="https://stackoverflow.com/questions/6935264/group-by-with-multiple-columns-using-lambda">useful SO question</a>, my problem is all about the <code>TruncateTime</code> inside the <code>GroupBy</code> clause. See explanations below :</p>
  
  <p>I'd like to use <code>DbFunctions.TruncateTime</code> in a multiple <code>GroupBy</code> clause, but it doesn't seem to work in my ASP.NET MVC5 project.</p>
  
  <p>Here is a first lambda I wrote, it gives me the total number of views per day for a set of data.<br>
  It gives me the expected result :</p>
  
  <pre><code>var qViews = dbContext.TABLE_C.Where(c =&gt; c.IdUser == 1234)
              .Join(dbContext.TABLE_V.Where(v =&gt; v.Date &gt; DbFunctions.AddMonths(DateTime.Now, -1)), c =&gt; c.Id, v =&gt; v.Id, (c, v) =&gt; new { c, v })
              .GroupBy(x =&gt; DbFunctions.TruncateTime(x.v.MyDateTimeColumn))
              .Select(g =&gt; new
              {
                  Date = (DateTime)g.Key,
                  NbViews = g.Count(),
              }).ToDictionary(p =&gt; p.Date, p =&gt; p.NbViews);
  </code></pre>
  
  <p>Result is something like that :</p>
  
  <blockquote>
    <p>...<br>
    Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| Views<br>
    03/07/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;15<br>
    03/08/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;8<br>
    03/09/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;23  </p>
  </blockquote>
  
  <p><br /><br />
  Now, I'd like a more detailled result, with the number of views per day AND PER ITEM on the same set of data.</p>
  
  <p>Here is what I'd like to write :</p>
  
  <pre><code>var qViews = dbContext.TABLE_C.Where(c =&gt; c.IdUser == 1234)
              .Join(dbContext.TABLE_V.Where(v =&gt; v.Date &gt; DbFunctions.AddMonths(DateTime.Now, -1)), c =&gt; c.Id, v =&gt; v.Id, (c, v) =&gt; new { c, v })
              .GroupBy(x =&gt; new { DbFunctions.TruncateTime(x.v.MyDateTimeColumn), x.c.Id})  // Issue #1
              .Select(g =&gt; new
              {
                  Date = g.Key.Date,  //Issue #2
                  NbViews = g.Count(),
              }).ToDictionary(p =&gt; p.Date, p =&gt; p.NbViews);
  </code></pre>
  
  <p>And I <strong>expected</strong> something like that :</p>
  
  <blockquote>
    <p>...<br>
    Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| Views | ID Item<br>
    03/07/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| 456789<br>
    03/07/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;&nbsp;| 845674<br>
    03/08/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| 325987<br>
    03/08/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| 548965<br>
    03/08/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| 222695<br>
    03/09/2018&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;23&nbsp;&nbsp;&nbsp;&nbsp;|  157896  </p>
  </blockquote>
  
  <p>So, this request have two issues (see comments above)</p>
  
  <p><strong>Issue #1</strong> : It seems I can't <code>GroupBy</code> multiple columns, which one of them use <code>DbFunctions</code>. If I use <code>.GroupBy(x =&gt; new { x.v.MyDateTimeColumn, x.c.Id })</code>, code compiles, but doesn't give me the expected result, as I want to group by date, not date + time</p>
  
  <p><strong>Issue #2</strong> : <code>Date = g.Key.Date,</code> seems wrong for the compiler. When I wrote <code>g.Key</code>, autocompletion only suggests me the <code>Id</code> column, but it doesn't see the truncated date.</p>
  
  <p>Why can't I <code>GroupBy</code> multiple columns, with one of them is a truncated Date ?<br>
  Is there any workaround ?</p>