I'm trying to take a DateTime field and create a List of DateObject, where DateObject has 2 properties (Year and Range). I want to identify the year of the DateTime field then... If it's even, I want to retain that year for Year. If it's odd, I want to change it to year - 1 for Year.
Then, based off that, I want to identify a string for Range that will look like "[EvenYear] to [EvenYear + 1]".
My data looks like this:
TableData
If the records look like:
I want the output List of DateObjects to look like this (distinct based off the Year):
I am so frustrated with figuring this out. I've tried a lot of code.
This is the code I left off with that kept failing:
var sessionYears = db.tblDates.GroupBy(x => (x.Date.Year % 2 == 0 ? x.Date.Year : x.Date.Year - 1)).ToList();
return sessionYears.Select(x => new List<DateObject> {
StartYear= x.Key,
Range = x.Select(y => y.Date.Year + " to " + (y.Date.Year + 1))
}
I've tried messing around with distinct and groupby, but I am clearly missing some steps, etc. I can't find anything online. Any help would be greatly appreciated!!! Thanks in advance.
You could create the list of DateRangeObject
first and after that get the distinct values. This will make your query much simpler.
return db.tblDates
.Select(x => new DateRangeObject {
StartYear = x.Date.Year % 2 == 0 ? x.Date.Year : x.Date.Year - 1,
EndYear = (x.Date.Year % 2 == 0 ? x.Date.Year : x.Date.Year - 1) + " - " + ((x.Date.Year % 2 == 0 ? x.Date.Year : x.Date.Year - 1) + 1);
})
.GroupBy(x => x.StartYear)
.Select(x => x.First())
.ToList();