private void UpdateMenuBeverages(string[] selectedBeverages, Menu menu) { if (selectedBeverages == null) { menu.Beverages = new List<Beverage>(); } var selectedBeveragesHS = new HashSet<string>(selectedBeverages); var menuBeveragesHS = new HashSet<int>(menu.Beverages.Select(b => b.BeverageID)); foreach (var beverage in db.Beverages) { if (selectedBeverages.Contains(beverage.BeverageID.ToString())) { if (!menuBeveragesHS.Contains(beverage.BeverageID)) { menu.Beverages.Add(beverage); } } else { if (menuBeveragesHS.Contains(beverage.BeverageID)) { menu.Beverages.Remove(beverage); } } } }
private void PopulateBeverages(Menu menu) { var allBeverages = db.Beverages; var SelectedBeverages = new HashSet<int> ( menu.Beverages.Select(b => b.BeverageID) ); var viewModel = new List<BeverageViewModels>(); foreach (var beverage in allBeverages) { viewModel.Add(new BeverageViewModels { BeverageID = beverage.BeverageID, Name = beverage.Name, Assigned = SelectedBeverages.Contains(beverage.BeverageID) }); } ViewBag.Beverages = viewModel; }