protected override bool Update(object newValue) { var electionKey = Page.GetElectionKey(); var changed = false; var stringValue = newValue.ToString(); // get the list of officeKeys var officeKeys = stringValue.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList(); // actualize any virtuals foreach (var officeKey in officeKeys) { Offices.ActualizeOffice(officeKey, Page.CountyCode, Page.LocalCode); } // create a dictionary of office keys from the updated tree var officeKeyDictionary = officeKeys .ToDictionary(ActualizeOfficeKey, key => false, StringComparer.OrdinalIgnoreCase); // get the current list of office keys for the election var offices = ElectionsOffices.GetOfficeKeysData(electionKey); // delete any that aren't in the dictionary (and from // ElectionsPoliticians too), and mark those that are there // so we know what to add foreach (var office in offices) { var officeKey = office.OfficeKey; if (officeKeyDictionary.ContainsKey(officeKey)) { officeKeyDictionary[officeKey] = true; } else { LogElectionsOfficesDelete(electionKey, officeKey); ElectionsOffices.DeleteByElectionKeyOfficeKey(electionKey, officeKey); ElectionsPoliticians.DeleteByElectionKeyOfficeKey(electionKey, officeKey); changed = true; } } // add offices from the dictionary that aren't marked foreach (var officeKey in officeKeyDictionary.Where(kvp => !kvp.Value) .Select(kvp => kvp.Key)) { LogElectionsOfficesInsert(electionKey, officeKey); InsertElectionOffice(electionKey, officeKey); changed = true; } LoadControl(); return(changed); }