[Solved] C# – How Get days from given month with the previous/next days to fill List


Using this DateTime Extension:

 static class DateExtensions
    {
        public static IEnumerable<DateTime> GetRange(this DateTime source, int days)
        {
            for (var current = 0; current < days; ++current)
            {
                yield return source.AddDays(current);
            };
        }

        public static DateTime NextDayOfWeek(this DateTime start, DayOfWeek dayOfWeek)
        {
            while (start.DayOfWeek != dayOfWeek)
                start = start.AddDays(-1);

            return start;
        }
    }

In my Class I use this:

    var numberOfDays = 42;

    DateTime startDate = new DateTime(year, month, 1).NextDayOfWeek(DayOfWeek.Monday);
    var dates = startDate.GetRange(numberOfDays)
        .Select(date => new CalendarDaysItem(date.Month, date.Day))
                         .ToList();

Define const to get number days;
Get days with start day week as monday;

solved C# – How Get days from given month with the previous/next days to fill List