Given a list of random dates, is there an elegant C# way (LINQ or otherwise) to extract the last date in the list, grouped by week, month, quarter or year?
I hope that's clear but if not, using this example set of dates:
- Mon 01/01/2018
- Tue 02/01/2018
- Thu 04/01/2018
- Tue 09/01/2018
- Thu 11/01/2018
- Fri 12/01/2018
- Mon 22/01/2018
- Tue 23/01/2018
- Wed 24/01/2018
- Wed 31/01/2018
- Thu 01/02/2018
- Tue 27/02/2018
Extracting maximum by week would yield
- Thu 04/01/2018 = the last date in the first week in the sample
- Fri 12/01/2018 = the last date in the second week in the sample
- Wed 24/01/2018 = etc.
- Thu 01/02/2018
- Tue 27/02/2018
And extracting maximum by month would yield
- Wed 31/01/2018 = the last date in January in the sample
- Tue 27/02/2018 = the last date in February in the sample
I need to be able to do this extraction by week number, calendar month, calendar quarter and calendar year.
I'm not even sure how to begin this. So at present I have no code to share.
Any ideas will be very welcome.
CLARIFICATION: the weekday names are included here for the benefit of us humans. My actual date is proper DateTime
types. So the code doesn't need to parse Strings to DateTimes.
Additionally, I can easily write something like this without LINQ. But what I am hoping for is an elegant solution using LINQ or some other clever trick.