private void UpdateCouponCategories()
        {
            int[] currentCategoryIds = GetCategoryIds();
            if (EqualIdCollections(currentCategoryIds, _pendingCategoryIds))
            {
                return;
            }
            if (currentCategoryIds != null)
            {
                List <CouponCategory> couponCategories = new List <CouponCategory>(CouponCategory.GetAll(ActiveCoupon.Id));

                // Remove any in current, that doesn't exist in pending
                foreach (CouponCategory existing in
                         from id in currentCategoryIds
                         where _pendingCategoryIds == null || !_pendingCategoryIds.Any(testId => testId == id)
                         select CouponCategory.FindByCategoryId(couponCategories, id))
                {
                    CouponCategory.Delete(existing.Id);
                }
            }

            // Add any missing pending to the current
            if (_pendingCategoryIds == null)
            {
                return;
            }
            foreach (int id in _pendingCategoryIds
                     .Where(id => !(currentCategoryIds != null && currentCategoryIds.Any(testId => testId == id))))
            {
                // Add a new CouponCategory for this Coupon
                CouponCategory.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);
                }
            }
        }