//deleting a Hall public bool RemoveHall(Hall hall) { //check if Hall exists in database var hallToRemove = Enumerable.FirstOrDefault <Hall>(_hallRepository.Halls(), h => h.HallId == hall.HallId); //checking if building has more than 1 hall if (Enumerable.Count <Hall>(_hallRepository.Halls(), h => h.BuildingId == hallToRemove.BuildingId) <= 1) { return(false); } //checking if Hall still has reservations if (hallToRemove.Reservations?.Any() == true) { return(false); } //get list of all OpeningHours used by this Hall before removing var openingHoursList = Enumerable.Where <HallOpeningHours>(_hallRepository.HallOpeningHours(), hoh => hoh.HallId == hall.HallId).ToList(); var hallRemoved = _hallRepository.RemoveHall(new Hall() { HallId = hallToRemove.HallId }); if (hallRemoved) { foreach (HallOpeningHours hoh in openingHoursList) { int openingHourCount = Enumerable.Count <HallOpeningHours>(_hallRepository.HallOpeningHours(), h => h.OpeningHoursId == hoh.OpeningHoursId); //remove entry in OpeningHours table if it is no longer used if (openingHourCount == 0) { RemoveOpeningHours(new OpeningHours { OpeningHoursId = hoh.OpeningHoursId }); } } return(hallRemoved); } return(false); }