Reputation: 277
Here is my dictionary.
var dic = new Dictionary<string, Dictionary<string, int>>();
I need to get the value of the int
in the inner dictionary.
foreach (var country in dic)
{
output.AppendFormat("{0} (total population: {1})", country.Key, HERE);
}
Any help?
Upvotes: 0
Views: 218
Reputation: 1390
Try to run this sample in degugger:
var dic = new Dictionary<string, Dictionary<string, int>>();
var cities = new Dictionary<string, int>();
cities.Add("Kiev", 6000000);
cities.Add("Lviv", 4000000);
dic.Add("Ukraine", cities);
var totalPopulationByCountry = dic.ToDictionary(x => x.Key, y => y.Value.Values);
var sumPopulationByCountry = dic.ToDictionary(x => x.Key, y => y.Value.Values.Sum());
should be what you need
Upvotes: 1
Reputation: 3099
If you want a population sum ("total popuplation"), you can use:
var sum = country.Value.Values.Sum();
output.AppendFormat("{0} (total population: {1})", country.Key, sum);
This uses LINQ, so you are required to have
using System.Linq;
in your source file.
Upvotes: 2