title: How to get hourly averages of my data
body: <p>I have a table called 'Samples' with the following columns: Id, Type, Quantity, Unit, SampleTime. </p>
  
  <p>I have the following statement that calculates the daily weight average:</p>
  
  <pre><code>weightchart.data = db.Samples
      .Where(n =&gt; n.Type == "weight")
      .GroupBy(n =&gt; DbFunctions.TruncateTime(n.SampleTime))
      .Select(item =&gt; new ChartData()
  {
      timestamp = item.Key,
      average = item.Average(a =&gt; a.Quantity),
      max = item.Max(a =&gt; a.Quantity),
      min = item.Min(a =&gt; a.Quantity),
      count = item.Count()
  }).OrderBy(n =&gt; n.timestamp).ToList();
  charts.Add(weightchart);
  </code></pre>
  
  <p>Now I would like to get the hourly average. I can't figure out how to do this. I'm new to c#, linq and entities.</p>
  
  <p><strong>Update:</strong> I've updated my statement to this:</p>
  
  <pre><code>weightchart.data = db.Samples.Where(n =&gt; n.Type == "weight").GroupBy(n =&gt; new { Date = n.SampleTime.Date, Hour = n.SampleTime.Hour }).Select(item =&gt; new ChartData()
  {
      timestamp = new DateTime(item.Key.Date.Year, item.Key.Date.Month, item.Key.Date.Day, item.Key.Date.Hour, 0, 0),
      average = item.Average(a =&gt; a.Quantity),
      max = item.Max(a =&gt; a.Quantity),
      min = item.Min(a =&gt; a.Quantity),
      count = item.Count()
  }).OrderBy(n =&gt; n.timestamp).ToList();
  charts.Add(weightchart);
  </code></pre>
  
  <p>However I get this exception:</p>
  
  <blockquote>
    <p>An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
    Additional information: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.</p>
  </blockquote>