How to make a collection of values for the same key in a dictionary?

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 2008
Suppose we have an entity by the name "ContinentCountry" that will hold the countries based on the continents being provided

public class ContinentCountry
{
public string ContinentName { get; set; }

public string CountryValues { get; set; }
}


Now, let us prepare a collection by entering some items as under

var continentCountryCollection = new List<ContinentCountry>();

continentCountryCollection.Add(new ContinentCountry { ContinentName = "Asia", CountryValues = "India" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Asia", CountryValues = "Pakistan" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Asia", CountryValues = "Srilanka" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Asia", CountryValues = "Bangaladesh" });

continentCountryCollection.Add(new ContinentCountry { ContinentName = "South America", CountryValues = "Argentina" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "South America", CountryValues = "Chile" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "South America", CountryValues = "Colombia" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "South America", CountryValues = "Brazil" });

continentCountryCollection.Add(new ContinentCountry { ContinentName = "Europe", CountryValues = "Norway" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Europe", CountryValues = "France" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Europe", CountryValues = "Spain" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Europe", CountryValues = "Belgium" });

continentCountryCollection.Add(new ContinentCountry { ContinentName = "Africa", CountryValues = "Ghana" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Africa", CountryValues = "South Africa" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Africa", CountryValues = "Uganda" });
continentCountryCollection.Add(new ContinentCountry { ContinentName = "Africa", CountryValues = "Somalia" });


Now, what we need to do is that, for every unique Continent Name in the collection, we should have an entry for multiple CountryValues and that will be in the dictionary

e.g. In a dictionary object of type <string, List<string>>, only one time "Asia" will appear but it will have 4 values in the Value Collection viz."India","Pakistan","Srilanka","Bangaladesh".

Here is my attempt to solve the same

Attempt 1:

var result = continentCountryCollection.GroupBy(x => x.ContinentName).ToDictionary(x => x.Key, x => x.Select(y => y.CountryValues).ToList());



Attempt 2:

var dictResult = new Dictionary<string, List<string>>();
foreach (var element in continentCountryCollection)
{
List<string> values;
if (!dictResult.TryGetValue(element.ContinentName, out values))
{
values = new List<string>();
dictResult.Add(element.ContinentName, values);
}
values.Add(element.CountryValues);
}

Hope this will be helpful.

Comments or Responses

Login to post response