私の辞書です。内部辞書の価値を得るには?
var dic = new Dictionary<string, Dictionary<string, int>>();
私は内側の辞書にint
の値を取得する必要があります。
foreach (var country in dic)
{
output.AppendFormat("{0} (total population: {1})", country.Key, HERE);
}
助けが必要ですか?
私の辞書です。内部辞書の価値を得るには?
var dic = new Dictionary<string, Dictionary<string, int>>();
私は内側の辞書にint
の値を取得する必要があります。
foreach (var country in dic)
{
output.AppendFormat("{0} (total population: {1})", country.Key, HERE);
}
助けが必要ですか?
あなたは人口の合計("総popuplation"を)したい場合は、あなたが使用することができます。
var sum = country.Value.Values.Sum();
output.AppendFormat("{0} (total population: {1})", country.Key, sum);
これは、LINQを使用していますので、あなたがあなたの元に
using System.Linq;
が要求されていますファイル。
ありがとうございます。神のお恵みがありますように。 –
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());
あなたは
'DICで' country.Key'で 'country.Value'は、それ自体が、'辞書 '必要なものでなければなりません'。内部辞書の*各*には多くの* int値が潜在的にあります。まさにあなたの目的がここにあるのは明らかではありません。 –
この場合、int型の内部ディクショナリの値を取得する必要があります。 –
dic ["abc"] ["xyz"] – jdweng