If you're counting every day as a working day, your code already works. Abbreviating things a bit you've got:
Enumerable.Range(0, 1 + (to - from).Days)
.Select(offset =>(from).AddDays(offset)).Count()
but the Select
is redundant as you're only doing a Count
afterwards anyway, so:
Enumerable.Range(0, 1 + (to - from).Days).Count()
but all you're doing here is counting the numbers the Range(int start, int count)
function has generated, so all the enumeration was pointless because you've specified this count in the first place, i.e.
1 + (to - from).Days
However, the enumeration would be useful if you wanted to exclude weekends etc. using some to-be-defined function, e.g.
Func<DateTime, bool> isWorkDay = x =>
{
if(x.DayOfWeek == DayOfWeek.Saturday) return false;
if(x.DayOfWeek == DayOfWeek.Sunday) return false;
if(x.Day == 25 && x.Month == 12) return false;
// etc.
return true;
};
Enumerable.Range(0, 1 + (to - from).Days)
.Where(x => isWorkDay(from.AddDays(x))).Count()