Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

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

May 11 2012 12:00AM by Niladri Biswas   

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>();
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.

Tags: C#,Dictionary,Collection


Niladri Biswas
7 · 21% · 6720
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"How to make a collection of values for the same key in a dictionary?" rated 5 out of 5 by 3 readers
How to make a collection of values for the same key in a dictionary? , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]