using System.Collections.Generic;
public static void Main()
List<Period> periodList = PreparePeriods();
List<DateTime> startDates = periodList
.Where(p => p.StartDate.HasValue)
.Select(p => p.StartDate.Value)
List<DateTime> endDates = periodList
.Where(p => p.EndDate.HasValue)
.Select(p => p.EndDate.Value)
startDates.Where(s => s > endDates.Max())
.ForEach(s => periodList.Add(new Period() { StartDate = s, EndDate = null }));
endDates.Where(e => e < startDates.Min())
.ForEach(e => periodList.Add(new Period() {StartDate = null, EndDate = e}));
startDates.Where(s => s < endDates.Max())
.ForEach(s => periodList.Add(new Period()
EndDate = endDates.Where(e => e > s).Min()
periodList = periodList.OrderBy(p => p.StartDate).ToList();
PrintPeriods(periodList);
static List<Period> PreparePeriods()
List<Period> periodList = new List<Period>
EndDate = new DateTime(year, 12, 1),
StartDate = new DateTime(year, 12, 2),
EndDate = new DateTime(year, 12, 4),
StartDate = new DateTime(year, 12, 6),
EndDate = new DateTime(year, 12, 8),
StartDate = new DateTime(year, 12, 9),
EndDate = new DateTime(year, 12, 10),
StartDate = new DateTime(year, 12, 11),
static void PrintPeriods(List<Period> periodList)
Console.WriteLine(string.Format("{0} | {1}",
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }