I have a list of dates like this and want to make sure that all dates within that list are within the same month and year. How would I accomplish that?
// Should yield true
IEnumerable<DateTime> dates1 = new List<DateTime>() { new DateTime(2022, 11, 30), new DateTime(2022, 11, 14), new DateTime(2022, 11, 2) };
// Should yield false
IEnumerable<DateTime> dates2 = new List<DateTime>() { new DateTime(2022, 11, 30), new DateTime(2022, 11, 14), new DateTime(2022, 10, 2) };
>Solution :
You can compare them with the first:
DateTime firstDate = dates1.First();
bool allSameMonthAndYear = dates1
.All(d => d.Year == firstDate.Year && d.Month == firstDate.Month);