private void UpdateCouponItems() { int[] currentItemIds = GetItemIds(); if (EqualIdCollections(currentItemIds, _pendingItemIds)) { return; } if (currentItemIds != null) { List <CouponItem> couponItems = new List <CouponItem>(CouponItem.GetAll(ActiveCoupon.Id)); // Remove any in current, that doesn't exist in pending foreach (CouponItem existing in from id in currentItemIds where _pendingItemIds == null || !_pendingItemIds.Any(testId => testId == id) select CouponItem.FindByItemId(couponItems, id)) { CouponItem.Delete(existing.Id); } } // Add any missing pending to the current if (_pendingItemIds != null) { foreach (int id in _pendingItemIds .Where(id => !(currentItemIds != null && currentItemIds.Any(testId => testId == id)))) { // Add a new CouponCategory for this Coupon CouponItem.Add(ActiveCoupon.Id, id); } } }
private void UpdateCoupon() { if (editorControl.UpdateCoupon()) { SetEditMode(false); if (listBox1.SelectedIndex >= 0) { var item = (FormattedListBoxItem)listBox1.SelectedItem; item.Set(editorControl.ActiveCoupon, editorControl.ActiveCoupon.Description); } else { var listItem = new FormattedListBoxItem( editorControl.ActiveCoupon, editorControl.ActiveCoupon.Description, true); listBox1.Items.Add(listItem); listBox1.SelectedItem = listItem; listBox1.ScrollIntoView(listItem); buttonDelete.IsEnabled = true; } } if (editorControl.ActiveCoupon != null) { var couponCategories = new List <CouponCategory>(CouponCategory.GetAll(editorControl.ActiveCoupon.Id)); foreach (int id in editorControl.couponCategorySelectionControl.SelectedCategoryIds .Where(id => CouponCategory.FindByCategoryId(couponCategories, id) == null)) { CouponCategory.Add(editorControl.ActiveCoupon.Id, id); } foreach (CouponCategory category in couponCategories .Where(category => !editorControl.couponCategorySelectionControl.SelectedCategoryIds .Contains(category.CategoryId))) { CouponCategory.Delete(editorControl.ActiveCoupon.Id, category.CategoryId); } var couponItems = new List <CouponItem>(CouponItem.GetAll(editorControl.ActiveCoupon.Id)); foreach (int id in editorControl.couponItemSelectionControl.SelectedItemIds .Where(id => CouponItem.FindByItemId(couponItems, id) == null)) { CouponItem.Add(editorControl.ActiveCoupon.Id, id); } foreach (CouponItem item in couponItems .Where(item => !editorControl.couponCategorySelectionControl.SelectedCategoryIds .Contains(item.ItemId))) { CouponItem.Delete(editorControl.ActiveCoupon.Id, item.ItemId); } } }