public void PopulateAssignedCategoryData(Proiect_shopContext context, Product book) { var allCategories = context.Category; var bookCategories = new HashSet <int>( book.ProductCategories.Select(c => c.ProductID)); AssignedCategoryDataList = new List <AssignedCategoryData>(); foreach (var cat in allCategories) { AssignedCategoryDataList.Add(new AssignedCategoryData { CategoryID = cat.ID, Name = cat.CategoryName, Assigned = bookCategories.Contains(cat.ID) }); } }
public void UpdateBookCategories(Proiect_shopContext context, string[] selectedCategories, Product bookToUpdate) { if (selectedCategories == null) { bookToUpdate.ProductCategories = new List <ProductCategory>(); return; } var selectedCategoriesHS = new HashSet <string>(selectedCategories); var bookCategories = new HashSet <int> (bookToUpdate.ProductCategories.Select(c => c.Category.ID)); foreach (var cat in context.Category) { if (selectedCategoriesHS.Contains(cat.ID.ToString())) { if (!bookCategories.Contains(cat.ID)) { bookToUpdate.ProductCategories.Add( new ProductCategory { ProductID = bookToUpdate.ID, CategoryID = cat.ID }); } } else { if (bookCategories.Contains(cat.ID)) { ProductCategory courseToRemove = bookToUpdate .ProductCategories .SingleOrDefault(i => i.CategoryID == cat.ID); context.Remove(courseToRemove); } } } }