public static DateTime NextByDayOfWeek(this DateTime target, IEnumerable<DayOfWeek> validDaysOfWeek)
{
validDaysOfWeek = validDaysOfWeek
.OrderBy(item => item);
var nextDayOfWeek = validDaysOfWeek
.Where(item => item > target.DayOfWeek)
.FirstOrDefault();
return nextDayOfWeek != 0
? target.AddDays(nextDayOfWeek - target.DayOfWeek)
: target.AddDays((validDaysOfWeek.First() + 7) - target.DayOfWeek);
}
Usage:
DateTime.Today.NextByDayOfWeek(new[] { DayOfWeek.Monday, DayOfWeek.Friday })
I don't think there are any possible edge cases here, at least some initial tests don't reveal any problem.
I think it's short and readable, but readability is often hard to judge when you're the author.
Any remarks?