I currently have 2 dictionaries with ard 30,000keys.
I am currently using two foreach loops to find the unique keys in dict1. And I write the unique keys in dict2 to another dictionary(3).
If the keys match i perform a check to see if the value are the same. And print if they are not the same.
Is it better to use foreach, it seems to be affecting the performance. Is there any other faster solution or built in functions? Ir shd I try the dict.contains method.
Dict1 Dict2
Keys Values Keys Values
S0111 00000 S0000 00010
S0000 00010 S0020 00015
S0020 00015 S0040 00150
S0040 00200 S0050 00250
foreach (KeyValuePair<string, string> sourceRow in sourceData)
{
foreach (KeyValuePair<string, string> dumpRow in dumpData)
{
// A in C, skip
if (sourceRow.Key == dumpRow.Key)
{
Is_Unique_Address = false;
if (sourceRow.Value != dumpRow.Value)
{
Data_Mismatch_Criteria(sourceRow.Key, sourceRow.Value, dumpRow.Key, dumpRow.Value);
}
}
}
}
This code enables you to select keys from dict2, which are not contained in dict1.
var dict1 = new Dictionary<string, string>();
var dict2 = new Dictionary<string, string>();
// Add data to both dictionaries
var uniqueKeys = dict2.Keys.Except(dict1.Keys);
Is that what you need?
EDIT: Please note, that the code above selects all keys from dict2 not contained in dict1. If you need to check that both dictionaries have different sets of keys (and I assume that it may be the case), then you can use:
var areTheSame = dict2.Keys.Any(x => !dict1.ContainsKey(x))
|| dict1.Keys.Any(x => !dict2.ContainsKey(x));
EDIT 2: After OP edit, I now know what you need:
var differentValues = dict2.Where(pair => dict1.ContainsKey(pair.Key))
.Select(pair => new
{
ValueA = pair.Value,
ValueB = dict1[pair.Key],
Key = pair.Key
})
.Where(x => x.ValueA != x.ValueB)
.ToList();
foreach (var differentValue in differentValues)
{
Console.WriteLine(differentValue);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With