0

I've a List of TimeInterval data that are 30 minute apart as shown below:

List<TimeInterval> testData = new List<TimeInterval>();
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; }

    }

Could someone please suggest the best way to aggregate the above data to :

  1. Hour
  2. Day
  3. Week
  4. Month
  5. Year

I tried with suggested solutions here but none of them seem to cater the above requirements. Thanks for your help.

The expected result for Hour for given example above is:

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 }

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.

Community
  • 1
  • 1
Nil Pun
  • 17,035
  • 39
  • 172
  • 294
  • 3
    Could you please be a bit more precise? Why does TimeInterval have Start and StartDate properties? How do you want to aggregate? What do you want to aggregate? Maybe you want to sum the timespan between start and end of each TimeInterval, e.g. 30 min + 30 min + ... = X min = Y hours...) Or do you want to count the number of different days, e.g. 2012-05-09 + 2012-05-10 = 2 days? Or...?? – Stephan Bauer May 09 '12 at 06:13

1 Answers1

1
List<Interval> CreatHourIntervals(DateTime start, int count, int intervalValue, int val)
{
    return     Enumerable.Range(0,count)
               .Select(x=> new Interval
                               {
                                 StartDate = start.AddHours(x*intervalValue), 
                                 EndDate = start.AddHours((x + 1)*intervalValue),
                                 value = val
                               }
                         ).ToList(); 
}

Description

start: is start date for the first item in the interval.

count: is the number of intervals you want.

intervalValue: is difference between intervals, e.g in hour case, this can vary between 1,12 (also it can be fractional). e.g if you set it to 2, each interval length is 2 hour.

For other types you can write related method yourself.

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83