C# Dictionary Help ASAP last lab duplicate keys

A word from our sponsor:

The Breast Form Store Halloween Sale Banner Ad (Save up to 60% off)
Printer-friendly version

Author: 

I have two dictionaries and i need to deal with the duplicate key issue.

for the visual studio project files
https://github.com/QKurtz/Lab12

private void CurrencyCalculatorForm_Load(object sender, EventArgs e)
{
try
{
////use this in second project for dictionary
ExchangeRateService exchangeRate =
new ExchangeRateService(EXCHANGE_RATE_URL);
CountryCodesToRates countryCodesToRate =
exchangeRate.DeserializeJsonData();

//dictionary with currency code and currency name
ExchangeRateService exchangeName =
new ExchangeRateService(CURRENCY_NAME_URL);
CountryCodesToNames countryCodesToName =
exchangeName.DeserializeJsonData();
//CompositeDictionary(countryCodesToRate.Rates, countryCodesToNames.Base);
//PopulateListView(countryCodesToRate.Rates);
dollarListBox.Items.AddRange(Dollar.GetDollarsList());

Dictionary results = new Dictionary();

for (int i = 0; i < countryCodesToRate.Rates.Count; i++)
{
string currencyName = "";
decimal conversionRate = 0;
foreach (KeyValuePair keyValuePair in countryCodesToName)
{
currencyName = keyValuePair.Value;
//results[keyValuePair.Key] = keyValuePair.Value;
}

foreach (KeyValuePair keyValuePair1 in countryCodesToRate.Rates)
{
conversionRate = keyValuePair1.Value;
//results[keyValuePair1.Value] = keyValuePair1.Value;
}

results.Add(currencyName, conversionRate);
var duplicateValues = results.GroupBy(x => x.Value).Where(x => x.Count() > 1);

if (results.Equals(duplicateValues))
{
results.Remove(currencyName);
}
else
{

}
}
PopulateListView(results);
}
Hugs:)
Michelle SidheElf Amaianna

Comments

Both of your beginning

Both of your beginning Dictionaries have countrycurrencycode as the key. I presume they are the same in each dictionary.
So, loop through all of the keys, for each key, get the values from each dictionary that correspond to that key, and put them into a new dictionary that you want as the result.

--Brandon Young

im not sure that would work,

im not sure that would work, will try to work out the logistics

hugs :)
Michelle SidheElf Amaianna

Use the features of the Dictionary class.

You should not need the 2 foreach loops for the key value pairs. You are using Dictionary objects. So use the features of the Dictionary class to solve the problem. The dictionary class makes a lookup table based on the key, So use the keys to find the corresponding values in each dictionary

Here is Microsoft's documentation on the Dictionary object

//Here is my take on getting the values from the 2 dictionaries together

/* The Dictionary object has a Keys property to get the collection of keys: */
Dictionary.KeyCollection keyColl =
countryCodesToName.Keys;

/* Now that you have the collection of keys, loop through the keys and use the key to look up the corresponding values in each Dictionary together and add those values as a new key value pair in the result dictionary */
foreach( string key in keyColl )
{
string rate = countryCodesToRate.Rates[key];
string countryName = countryCodesToName[key];
results.Add(countryName,rate);
}

--Brandon Young

Works almost perfectly

It works just had to swap decimal in place of string for the data type for rate.
thank you

hugs :)
Michelle SidheElf Amaianna