public List <Appointment> GetAvailableAppointments(DateTime date, int clinicId) { var clinicOpeningHours = _clinicRepository .GetClinicBusinessHours(clinicId); var appointmentDay = clinicOpeningHours .FirstOrDefault(s => s.Day.ToString() == date.DayOfWeek.ToString()); var appointmentsList = new List <Appointment>(); DateTime startTime = Convert.ToDateTime(appointmentDay.OpenTime); DateTime endTime = Convert.ToDateTime(appointmentDay.CloseTime); double openTime = Convert.ToDouble(startTime.Hour) + Convert.ToDouble(startTime.Minute) / 60; double closeTime = Convert.ToDouble(endTime.Hour) + Convert.ToDouble(endTime.Minute) / 60; int duration = 30; double increment = 0.5; while (openTime < closeTime) { string startTimeString = startTime.ToString("hh:mm tt"); var appointmentExists = _appDbContext.Appointments .Any(s => s.Date == date && s.StartTime == startTimeString); if (!appointmentExists) { appointmentsList.Add(new Appointment() { Date = date, StartTime = startTimeString, EndTime = startTime.AddMinutes(duration).ToString("hh:mm tt") }); } startTime = startTime.AddMinutes(duration); openTime += increment; } return(appointmentsList); }