What's the most simple way to group data over time, by month?
In the example below, there's a list of games where each have the date played and the city the game was played in. If we want to get the number of games played per month, by grouping them by month, is it sensible to use a composite year-month key like I am? Any other approaches? Including those that don't require resorting to composite grouping key?
public static void GamesByMonth ()
{
var games = new List<Game>()
{
new Game() { Date = new DateTime( 2010, 11, 15 ), City = "Denver"},
new Game() { Date = new DateTime( 2011, 1, 11 ), City = "Chicago"},
new Game() { Date = new DateTime( 2011, 1, 10 ), City = "Houston"},
new Game() { Date = new DateTime( 2011, 3, 21 ), City = "Atlanta"},
new Game() { Date = new DateTime( 2011, 4, 18 ), City = "Denver"},
new Game() { Date = new DateTime( 2011, 4, 29 ), City = "Boston"}
};
var groupings =
from game in games
group game by new
{
game.Date.Year,
game.Date.Month
};
}
Note: I'm including year in the grouping key because, in this case, we want the trend over time as opposed to the absolute number of games played in a particular month, regardless of year.
var groupings =
from game in games
group game by new DateTime(game.Date.Year, game.Date.Month, 1)