Checking for duplicate keys using WPF and C#


Checking for duplicate keys using WPF and C#



I'm creating an application that will look AppSettings, pull those keys and their values up into a DataTable, use the DataTable to edit or add keys, check for duplicate keys once edited, and save them back to into AppSettings. I currently have three buttons. One is ProgrammedBy which pulls up a separate data table, a View App Settings within App.config and display it on a data table, and finally an Update Settings button that checks for duplicate keys after the information is entered on the DataTable then save the keys and their values back to AppSettings:


AppSettings


ProgrammedBy



AppView



Here is a list of the keys and values within in the App.config file. Note I have given a key a duplicate name:



App.Config



I'm using a nested for loop on the part where it checks for duplicate keys. The System.Collection library only checks the last duplicate key. It totally ignores the first one. When I drill down or hover my mouse over the items variable, Rows/Results View, within Visual Studio it only shows me a 5 key pair. There is obviously a 6th pair as seen above. Here is that for loop and for the purpose of debugging I have put a break point past the end of the for loop:



Nested For Loop within button instance



I need it to be able to throw an exception error when it finds the duplicate key. If at all possible throw the exception error message and highlight what that duplicate key is. Also if possible make the System.Collection library not skip the duplicate key or ignore the duplicate key.



Thank you for any help you might be able to provide. May God bless you for your hard work and kindness.





How do you read appSettings? use ConfigurationManager? It may use the last duplicate setting value, you should check it.
– WAKU
Jun 29 at 3:06



appSettings


ConfigurationManager





what should happen when a value with an existing key is entered by the user ? Erase the existing one or prevent its creation ?
– Spotted
Jun 29 at 11:39





Hi @Benny, did you have a chance to look at the below suggestion?
– Johan Aspeling
18 hours ago




1 Answer
1



You can use the following code to find duplicate keys:


// Cast the DataTable rows to a list, and select the 'Key' column's Value
var keys = items.Rows.Cast<DataRow>().ToList().Select(r => r["Key"]);

// Find the Duplicate keys
var duplicateKeys = keys.GroupBy(x => x)
.Where(group => group.Count() > 1)
.Select(group => group.Key);

// Throw the exception, containing the keys that are duplicated.
if (duplicateKeys.Count() > 0) {
throw new ConfigurationErrorsException($"Duplicate key(s) found: '{string.Join(", ", duplicateKeys)}' - please revise");
//EDIT: < VS2015: throw new ConfigurationErrorsException("Duplicate key(s) found: " + string.Join(", ", duplicateKeys) + " - please revise.");
}



The way you have to find the keys from the DataTable might differ, but it boils down to the same situation.





Please note I am using String interpolation to insert a variable in a string when the Exception is thrown. This is only available from VS2105 and above, if you do not have that, you can use the edited approach.
– Johan Aspeling
Jun 29 at 10:00





Also note this approach will replace all three for loops in your example.
– Johan Aspeling
Jun 29 at 10:08






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV