I have a small algorithm to check existing appointments date falls into a the new defined range of hours.
I have the feeling of being to bloated with unnecessary variables declarations, how could this be improved?
private bool HasMissingWorkingHour(IEnumerable<Appointment> appointments, Doctor doctor)
{
var appointmentDates = appointments.Select(x => x.Start).ToList();
var incomingWorkingDays = doctor.WorkingDays;
var matchingDays = appointmentDates.Where(d => incomingWorkingDays.Any(w => w.Day == d.DayOfWeek));
return (from scheduled in matchingDays
let workingRange = _appointmentsDoctorsBL.GetWorkingTimes(incomingWorkingDays, scheduled.DayOfWeek)
where scheduled.Hour <= workingRange.Start.Hour || scheduled.Hour >= workingRange.End.Hour
select scheduled).Any();
}
public TimeRange GetWorkingTimes(ICollection<WorkingDay> workingDays, DayOfWeek dayOfWeek)
{
var workStartTime = workingDays.Where(w => w.Day == dayOfWeek).Min(w => w.StartTime);
var workFinishTime = workingDays.Where(w => w.Day == dayOfWeek).Max(w => w.EndTime);
return new TimeRange(workStartTime, workFinishTime);
}