0

I am dealing with big data of years.

The data model is quite simple:

public class ValueData
{
    public DateTime TimeRecorded {get; set;}
    public double   ValueRecorded {get; set;}
}

After having a list of ValueData: List<ValueData> for years of data, I need to group the data based on: Year ==> contains data of 4 seasons: Season ==> A season contains 4 months ==> A month contains data of 4 weeks ==> A week contains data of 7 days based on the week calendar numbers of a year. Because I need to make a sum of data per year, per season, per month, per week and per day

How can I achieve this data classification? should I use LinQ?

halfer
  • 19,824
  • 17
  • 99
  • 186
olidev
  • 20,058
  • 51
  • 133
  • 197

2 Answers2

0

I guess you are looking for something like conditional groups but 2gbs of data will take a while to process. I guess it's ok for a one time parse and save the results but if you need to run this often you'll need a more appropriate solution.

Community
  • 1
  • 1
Marcom
  • 4,621
  • 8
  • 54
  • 78
0

I believe you want something along the lines of the following query:

var groups = data.GroupBy(v => new {Year = v.TimeRecorded.Year,
                                    Season = SeasonFromMonth(v.TimeRecorded.Month),
                                    Month = v.TimeRecorded.Month,
                                    Week = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(v.TimeRecorded, System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Monday),
                                    Day = v.TimeRecorded.Day});

With a helper function SeasonFromMonth that accepts the integer month and returns some value (probably an enum) indicating the season.

You can then select relevant subgroups with queries like:

var fallValues = groups.Where(g => g.Key.Season == Seasons.Fall);
var decemberValues = groups.Where(g => g.Key.Month == 12);
var firstOfMonth = groups.Where(g => g.Key.Day == 1);

and so on, or flatten the groups into a single list by adding a SelectMany clause (although the SelectMany will throw away the key information):

groups.Where(g => g.Key.Season == Seasons.Fall).SelectMany(g => g);
groups.Where(g => g.Key.Month == 12).SelectMany(g => g);
groups.Where(g => g.Key.Day == 1).SelectMany(g => g);
Zack Butcher
  • 1,046
  • 7
  • 12