private void UpdateCounselorCompounds(string[] selectedOptions, Compound compoundToUpdate) { if (selectedOptions == null) { compoundToUpdate.CounselorCompounds = new List <CounselorCompound>(); return; } var selectedOptionsHS = new HashSet <string>(selectedOptions); var compoundCounselors = new HashSet <int>(compoundToUpdate.CounselorCompounds.Select(b => b.CounselorID)); foreach (var s in _context.Counselors) { if (selectedOptionsHS.Contains(s.ID.ToString())) { if (!compoundCounselors.Contains(s.ID)) { compoundToUpdate.CounselorCompounds.Add(new CounselorCompound { CounselorID = s.ID, CompoundID = compoundToUpdate.ID }); } } else { if (compoundCounselors.Contains(s.ID)) { CounselorCompound specToRemove = compoundToUpdate.CounselorCompounds.SingleOrDefault(d => d.CounselorID == s.ID); _context.Remove(specToRemove); } } } }
private void UpdateCamperDietaryRestrictions(string[] selectedOptions, Camper camperToUpdate) { if (selectedOptions == null) { camperToUpdate.CamperDiets = new List <CamperDiet>(); return; } var selectedDietaryRestrictionsHS = new HashSet <string>(selectedOptions); var camperDietaryRestrictionsHS = new HashSet <int> (camperToUpdate.CamperDiets.Select(c => c.DietaryRestrictionID));//IDs of the currently selected dietaryRestrictions foreach (var cond in _context.DietaryRestrictions) { if (selectedDietaryRestrictionsHS.Contains(cond.ID.ToString())) { if (!camperDietaryRestrictionsHS.Contains(cond.ID)) { camperToUpdate.CamperDiets.Add(new CamperDiet { CamperID = camperToUpdate.ID, DietaryRestrictionID = cond.ID }); } } else { if (camperDietaryRestrictionsHS.Contains(cond.ID)) { CamperDiet dietaryRestrictionToRemove = camperToUpdate.CamperDiets.SingleOrDefault(c => c.DietaryRestrictionID == cond.ID); _context.Remove(dietaryRestrictionToRemove); } } } }