private bool ApplyEdits(EditViewModel viewModel, GMailLabelCleanup.Data.Models.Filters.Filter filter)
        {
            System.Diagnostics.Debug.Assert(viewModel != null);
            System.Diagnostics.Debug.Assert(filter != null);

            bool hasChanges = false;

            //Apply high level properties
            if (!string.Equals(filter.Description, viewModel.Description))
            {
                filter.Description = viewModel.Description;
                hasChanges = true;
            }

            foreach (var item in viewModel.ActionProperties.Concat(viewModel.CriteriaProperties).Where(o => o.IsCheckType))
            {
                item.Value = item.IsChecked ? "true" : "false";
            }

            /*
             * New properties:
             * ViewModel properties that are included and whose
             * name does not appear in the Filter.FilterProperties.
             * 
             * Updated properies:
             * ViewModel properties whose
             * name appears in the Filter.FilterProperties.
             */

            var existingPropertyNames = filter.FilterProperties
                .Select(o => o.Name)
                .ToHashSet();

            var allProperties = viewModel.CriteriaProperties
                .Concat(viewModel.ActionProperties)
                .ToArray();

            var newProperties = allProperties
                .Where(o => !existingPropertyNames.Contains(o.Name));
            var updatedProperties = allProperties
                .Where(o => existingPropertyNames.Contains(o.Name));

            var dateCreated = DateTime.UtcNow;

            foreach (var item in updatedProperties)
            {
                var matches = filter.FilterProperties
                   .Where(o => string.Equals(o.Name, item.Name) && (!string.Equals(o.Value, item.Value) || o.IsIncluded != item.IsIncluded))
                   .ToArray();

                foreach (var match in matches)
                {
                    match.IsIncluded = item.IsIncluded;
                    match.Value = item.Value;
                    hasChanges = true;
                }
            }
            foreach (var item in newProperties)
            {
                filter.FilterProperties.Add(new FilterProperty
                {
                    IsIncluded = item.IsIncluded,
                    Name = item.Name,
                    Value = item.Value,
                    DateCreatedUtc = dateCreated
                });
                hasChanges = true;
            }
            return hasChanges;
        }
        private void InitializeEditViewModel(EditViewModel viewModel, GMailLabelCleanup.Data.Models.Filters.Filter filter)
        {
            System.Diagnostics.Debug.Assert(viewModel != null);

            viewModel.Id = filter != null ? filter.FilterId : 0;
            viewModel.Description = filter != null ? filter.Description : string.Empty;

            InitializeEditSelectLists(viewModel);
            InitializeEditProperties(viewModel, filter != null ? filter.FilterProperties : new FilterProperty[] { });
        }