private void UpdateTypeCategories(IEnumerable <int> SelectedCategories, ServiceType serviceType) { List <Category> selectedCategories = (SelectedCategories == null || !SelectedCategories.Any()) ? new List <Category>() : CategoryRepository.Items.Where(s => SelectedCategories.Contains(s.Id)).ToList(); IEnumerable <Category> categorySet = selectedCategories.Union(serviceType.Categories.ToArray()); foreach (Category category in categorySet) { if (!selectedCategories.Contains(category)) { ServiceTypeRepository.DeleteLink(serviceType, category); } else if (!serviceType.Categories.Contains(category)) { ServiceTypeRepository.AddLink(serviceType, category); } } }