using System.Collections.Generic;
public DateTime ActiveDate;
public DateTime ExpireDate;
public static void Main()
List<Dates> datesList = new List<Dates>();
datesList.Add(new Dates{Name = "A", ActiveDate = new DateTime(2017, 8, 20), ExpireDate = new DateTime(2017, 8, 23)});
datesList.Add(new Dates{Name = "B", ActiveDate = new DateTime(2017, 8, 20), ExpireDate = new DateTime(2017, 8, 22)});
datesList.Add(new Dates{Name = "C", ActiveDate = new DateTime(2017, 8, 22, 23, 0, 0), ExpireDate = new DateTime(2017, 8, 26)});
datesList.Add(new Dates{Name = "D", ActiveDate = new DateTime(2017, 8, 20), ExpireDate = new DateTime(2017, 8, 23)});
var compareRanges = datesList.Select(d => GetDateRange(d)).ToList();
var result = CompareDates(compareRanges, 3);
Console.WriteLine("Overlaps");
foreach(var dt in result)
Console.WriteLine(dt.ToString("M/d/yyyy"));
var maxOverlap = GetLongestOverlap(compareRanges);
Console.WriteLine("Most overlaps: " + maxOverlap.Item2);
foreach(var dt in maxOverlap.Item1)
Console.WriteLine(dt.ToString("M/d/yyyy"));
private static DateTime[] GetDateRange(Dates dt)
var start = dt.ActiveDate.Date;
var end = dt.ExpireDate.Date;
return Enumerable.Range(0, Convert.ToInt32((end.Date - start.Date).TotalDays) + 1)
.Select(e => start.Date.AddDays(e)).ToArray();
private static List<DateTime> CompareDates(List<DateTime[]> compareRanges, int overlapThreshold = 1)
var grouped = compareRanges.SelectMany(r => r).GroupBy(d => d.Date);
var thresholdMatch = grouped.Where(g => g.Count() >= overlapThreshold)
private static Tuple<List<DateTime>, int> GetLongestOverlap(List<DateTime[]> compareRanges)
var grouped = compareRanges.SelectMany(r => r).GroupBy(d => d.Date);
var max = grouped.Max(g => g.Count());
var maxMatch = grouped.Where(g => g.Count() == max)
return new Tuple<List<DateTime>, int>(maxMatch, max);