Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 12203 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > How to make a collection of values for the same key in a dictionary? ...
Niladri.Biswas

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

 Code Snippet posted by: Niladri.Biswas | Posted on: 5/11/2012 | Category: C# Codes | Views: 701 | Status: [Member] | Points: 40 | Alert Moderator   
Ads

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.

Best Regards,
Niladri Biswas
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 11:26:04 PM