I would suggest you to have a extension method on the datetime object, so that you can exclude holidays at a later stage. We always start with weekends , and then you would ask for holidays. So the extension method would be a simpler way of placing this logic.
First extension method to check if it is a weekday.
public static bool WeekDay(this DateTime date)
{
return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday;
}
Second extension method to retrieve all the dates in the range. So that we can compare the dates if they fall on a weekday.
public static IEnumerable<DateTime> GetDatesInTheRange(this DateTime startdate, this DateTime enddate)
{
DateTime pointerDate = start;
while (pointerDate < end)
{
yield return pointerDate;
pointerDate = pointerDate.AddDays(1);
}
}
And then simply use with the following code:
List<DateTime> allDates = GetDatesInTheRange(calendar1date - calendar2date);
int leavesApplied = allDates.Count(day => day.WeekDay()).
Hope this helps.