public static List <DoctorViewModel> GetDoctorViewModelsFromPractice(CerebelloEntitiesAccessFilterWrapper db, Practice practice, DateTime localNow) { var usersThatAreDoctors = db.Users .Where(u => u.PracticeId == practice.Id) .Where(u => u.Doctor != null); var dataCollection = usersThatAreDoctors .Select(u => new { ViewModel = new DoctorViewModel() { Id = u.Id, Name = u.Person.FullName, UrlIdentifier = u.Doctor.UrlIdentifier, CRM = u.Doctor.CRM, MedicalSpecialty = u.Doctor.MedicalSpecialtyName, }, u.Doctor.MedicalEntityCode, u.Doctor.MedicalEntityJurisdiction, u.Doctor, u.Person.EmailGravatarHash, }) .ToList(); // Getting more doctor's informations: // Todo: this is going to become a problem in the future, because this info has no cache. // - next free time slot of each doctor; // - gravatar image. foreach (var eachItem in dataCollection) { if (!string.IsNullOrEmpty(eachItem.EmailGravatarHash)) { eachItem.ViewModel.ImageUrl = GravatarHelper.GetGravatarUrl(eachItem.EmailGravatarHash, GravatarHelper.Size.s16); } eachItem.ViewModel.MedicalEntity = string.Format( string.IsNullOrEmpty(eachItem.MedicalEntityJurisdiction) ? "{0}" : "{0}-{1}", eachItem.MedicalEntityCode, eachItem.MedicalEntityJurisdiction); // It is only possible to determine the next available time if the schedule of the doctor is already configured. if (eachItem.Doctor.CFG_Schedule != null) { var nextSlotInLocalTime = ScheduleController.FindNextFreeTimeInPracticeLocalTime(db, eachItem.Doctor, localNow); eachItem.ViewModel.NextAvailableTime = nextSlotInLocalTime.Item1; } } var doctors = dataCollection.Select(item => item.ViewModel).ToList(); return(doctors); }