title: Is this LINQ query with averaging and grouping by hour written efficiently?
body: <p>This is my first real-world LINQ-to-SQL query. I was wondering if I am making any large, obvious mistakes. </p>
  
  <p>I have a medium-large sized (2M+ records, adding 13k a day) table with data, dataTypeID, machineID, and dateStamp. I'd like to get the average, min, and max of data from all machines and of a specific dataType within a 4 hour period, going back for 28 days.</p>
  
  <p>E.g</p>
  
  <p>DateTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Avg&nbsp;&nbsp;&nbsp;Min&nbsp;&nbsp;&nbsp;Max<br/>
  1/1/10 12:00AM&nbsp;&nbsp;74.2&nbsp;&nbsp;72.1&nbsp;&nbsp;75.7<br/>
  1/1/10 04:00AM&nbsp;&nbsp;74.5&nbsp;&nbsp;73.1&nbsp;&nbsp;76.2<br/>
  1/1/10 08:00AM&nbsp;&nbsp;73.7&nbsp;&nbsp;71.5&nbsp;&nbsp;74.2<br/>
  1/1/10 12:00PM&nbsp;&nbsp;73.2&nbsp;&nbsp;71.2&nbsp;&nbsp;76.1<br/>
  etc..<br />
  1/28/10 12:00AM&nbsp;&nbsp;73.1&nbsp;&nbsp;71.3&nbsp;&nbsp;75.5<br/></p>
  
  <p>So far I have only been able to group the averages by 1 hour increments, but I could probably deal with that if the alternatives are overly messy.</p>
  
  <p>Code:</p>
  
  <pre><code>var q =
      from d in DataPointTable
      where d.dateStamp &gt; DateTime.Now.AddDays(-28) &amp;&amp; (d.dataTypeID == (int)dataType + 1)
      group d by new {
          d.dateStamp.Year,
          d.dateStamp.Month,
          d.dateStamp.Day,
          d.dateStamp.Hour
      } into groupedData
      orderby groupedData.Key.Year, groupedData.Key.Month, groupedData.Key.Day, groupedData.Key.Hour ascending
      select new {
          date = Convert.ToDateTime(
              groupedData.Key.Year.ToString() + "-" +
              groupedData.Key.Month.ToString() + "-" +
              groupedData.Key.Day.ToString() + " " + 
              groupedData.Key.Hour.ToString() + ":00"
              ),
          avg = groupedData.Average(d =&gt; d.data),
          max = groupedData.Max(d =&gt; d.data),
          min = groupedData.Min(d =&gt; d.data)
      };
  </code></pre>