We have a table with calculated data that groups sales by product, year and month, to provide fast querying for statistics.
My colleague argues that the year and month should be two separate fields, because a day is meaningless.
I want it as a date field, because using two separate fields leads to awkward code like this
var lastTwelveMonths = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-11);
var result = data.Where(item =>
(item.Year > lastTwelveMonths.Year
|| (item.Year == lastTwelveMonths.Year && item.Month >= lastTwelveMonths.Month));
instead of this
var lastTwelveMonths = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-11);
var result = data.Where(item => item.YearAndMonthAsDate >= lastTwelveMonths);
I understand his argument, but the code is harder to read and there's a higher chance on bugs when doing date/time calculations without using date/time objects. I also can't cast it to a DateTime
in the query because I'm using LINQ to Entities.
Which method is the better one?