2016-04-05 13 views
0

私の辞書です。内部辞書の価値を得るには?

var dic = new Dictionary<string, Dictionary<string, int>>(); 

私は内側の辞書にintの値を取得する必要があります。

foreach (var country in dic) 
{ 
    output.AppendFormat("{0} (total population: {1})", country.Key, HERE); 
} 

助けが必要ですか?

+1

'DICで' country.Key'で 'country.Value'は、それ自体が、'辞書 '必要なものでなければなりません'。内部辞書の*各*には多くの* int値が潜在的にあります。まさにあなたの目的がここにあるのは明らかではありません。 –

+0

この場合、int型の内部ディクショナリの値を取得する必要があります。 –

+0

dic ["abc"] ["xyz"] – jdweng

答えて

2

あなたは人口の合計("総popuplation"を)したい場合は、あなたが使用することができます。

var sum = country.Value.Values.Sum(); 
    output.AppendFormat("{0} (total population: {1})", country.Key, sum); 

これは、LINQを使用していますので、あなたがあなたの元に

using System.Linq; 

が要求されていますファイル。

+0

ありがとうございます。神のお恵みがありますように。 –

1

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

あなたは

関連する問題