protected override bool Update(object newValue) { //Parse the value from the UI tree if (!(newValue is string valueStr)) { return(false); } var offices = valueStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(officeStr => { var selected = officeStr[0] == '*'; var officeSplit = officeStr.Substring(selected ? 1 : 0).Split('='); var isRunoff = officeSplit[1].StartsWith("*", StringComparison.Ordinal); if (isRunoff) { officeSplit[1] = officeSplit[1].Substring(1); } return(new { Selected = selected, OfficeKey = officeSplit[0], IsRunoff = isRunoff, Ids = officeSplit[1].Split(',') }); }); var electionKey = Page.GetElectionKey(); var table = ElectionsPoliticians.GetWinnersData(electionKey); foreach (var o in offices) { var office = o; var politicians = table .Where(row => row.OfficeKey().IsEqIgnoreCase(office.OfficeKey)).ToList(); foreach (var politician in politicians) { if (office.IsRunoff) { var advance = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.AdvanceToRunoff != advance) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.AdvanceToRunoff, politician.AdvanceToRunoff, advance, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.AdvanceToRunoff = advance; politician.IsWinner = false; } else // non-runoff { var isWinner = office.Ids.Contains(politician.PoliticianKey, StringComparer.OrdinalIgnoreCase); if (politician.IsWinner != isWinner) { LogDataChange.LogUpdate(ElectionsPoliticians.Column.IsWinner, politician.IsWinner, isWinner, DateTime.UtcNow, electionKey, politician.OfficeKey, politician.PoliticianKey); } politician.IsWinner = isWinner; politician.AdvanceToRunoff = false; } } if (office.Selected) { var keys = politicians.Select(p => (politicianKey: p.PoliticianKey, runningMateKey: p.RunningMateKey)).ToList(); OfficesOfficials.UpdateIncumbents(office.OfficeKey, office.Ids, keys, UserSecurityClass, UserName); //// Update incumbents //var positions = Offices.GetPositionsDataByOfficeKey(office.OfficeKey)[0]; //var incumbents = OfficesOfficials.GetData(office.OfficeKey); //var politicianKeysToAdd = // new List<string>( // office.Ids.Where(id => id != "vacant" && id != Empty)); //foreach (var incumbent in incumbents) //{ // var index = // politicianKeysToAdd.FindIndex( // key => key.IsEqIgnoreCase(incumbent.PoliticianKey)); // if (index >= 0) politicianKeysToAdd.RemoveAt(index); // // we don't remove old incumbents for offices with // // Incumbents > ElectionPositions // else if (positions.ElectionPositions == positions.Incumbents) // incumbent.Delete(); //} //foreach (var keyToAdd in politicianKeysToAdd) //{ // var politician = // politicians.FirstOrDefault( // row => row.PoliticianKey.IsEqIgnoreCase(keyToAdd)); // var runningMateKey = politician == null // ? Empty // : politician.RunningMateKey; // incumbents.AddRow(office.OfficeKey, keyToAdd, runningMateKey, // Offices.GetStateCodeFromKey(office.OfficeKey), // Offices.GetCountyCodeFromKey(office.OfficeKey), // Offices.GetLocalKeyFromKey(office.OfficeKey), Empty, DateTime.UtcNow, // UserSecurityClass, UserName); // LogDataChange.LogInsert(OfficesOfficials.TableName, DateTime.UtcNow, // office.OfficeKey, keyToAdd); //} //// Update if any changes //var incumbentsChanged = // incumbents.FirstOrDefault(row => row.RowState != DataRowState.Unchanged) != // null; //if (incumbentsChanged) OfficesOfficials.UpdateTable(incumbents); } } // Update if any changes var winnersChanged = table.FirstOrDefault(row => row.RowState != DataRowState.Unchanged) != null; if (winnersChanged) { ElectionsPoliticians.UpdateTable(table, ElectionsPoliticiansTable.ColumnSet.Winners); } LoadControl(); return(winnersChanged); }