title: LINQ DateTime Aggregation with GroupBy and Group By
body: <p>I've  a List of TimeInterval data that are 30 minute apart as shown below:</p>
  
  <pre><code>List&lt;TimeInterval&gt; testData = new List&lt;TimeInterval&gt;();
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-09 6:00:00 AM"), End = DateTime.Parse("2012-05-09 6:30:00 AM"), Value = 1 });
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-09 6:30:00 AM"), End = DateTime.Parse("2012-05-09 7:00:00 AM"), Value = 1 });
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-09 7:00:00 AM"), End = DateTime.Parse("2012-05-09 7:30:00 AM"), Value = 1 });
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-09 7:30:00 AM"), End = DateTime.Parse("2012-05-09 8:00:00 AM"), Value = 1 });
  .....
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-10 7:00:00 AM"), End = DateTime.Parse("2012-05-10 7:30:00 AM"), Value = 20 });
  testData.Add(new TimeInterval() { Start = DateTime.Parse("2012-05-10 7:30:00 AM"), End = DateTime.Parse("2012-05-10 8:00:00 AM"), Value = 10 });
  ................... up to N number of N number of years.
  
  
  public class TimeInterval
      {
         public DateTime Start { get; set; }
         public DateTime End { get; set; }
         public decimal Value { get; set; }
  
      }
  </code></pre>
  
  <p>Could someone please suggest the best way to aggregate the above data to :</p>
  
  <blockquote>
    <ol>
    <li>Hour</li>
    <li>Day</li>
    <li>Week</li>
    <li>Month</li>
    <li>Year</li>
    </ol>
  </blockquote>
  
  <p>I tried with suggested solutions <a href="https://stackoverflow.com/questions/10498831/linq-aggregate-30-minute-interval-to-hour">here</a> but none of them seem to cater the above requirements. Thanks for your help.</p>
  
  <p>The expected result for Hour for given example above is:</p>
  
  <pre><code>Lists Of:
  
  TimeInterval() { Start = DateTime.Parse("2012-05-09 6:00:00 AM"), End = DateTime.Parse("2012-05-09 7:00:00 AM"), Value = 2 }
  TimeInterval() { Start = DateTime.Parse("2012-05-09 7:00:00 AM"), End = DateTime.Parse("2012-05-09 8:00:00 AM"), Value = 2 }
  ...
  TimeInterval() { Start = DateTime.Parse("2012-05-10 7:00:00 AM"), End = DateTime.Parse("2012-05-10 8:00:00 AM"), Value = 2 }
  </code></pre>
  
  <p>For Days, it will be total for the day.
  for week, it will be total for a whole week etc. basically sum the Value for given aggregation type.</p>