C# 无法从C中嵌套的SortedDictionary读取值
嗨 我在代码中使用嵌套的SortedDictionary作为SortedDictionary,但无法使用存储在此对象中的值。 请找到我正在使用的代码C# 无法从C中嵌套的SortedDictionary读取值,c#,C#,嗨 我在代码中使用嵌套的SortedDictionary作为SortedDictionary,但无法使用存储在此对象中的值。 请找到我正在使用的代码 SortedDictionary<string, SortedDictionary<string, int>> baseItemCounts = new SortedDictionary<string, SortedDictionary<string, int>>();
SortedDictionary<string, SortedDictionary<string, int>> baseItemCounts =
new SortedDictionary<string, SortedDictionary<string, int>>();
baseItemCounts.Add("1450", new SortedDictionary<string, int>());
baseItemCounts["1450"].Add("1450M", 15);
我想在屏幕上打印这些值。但我不知道如何访问它。
1450
1450M==15
请找人帮忙?baseItemCounts[1450][1450M]应该给你15,因为第一个索引会给你返回的值是第二个SortedDictionary,所以你只需要使用第二个索引从第二个SortedDictionary中获取值,这是你的值 要打印计数值,请执行以下操作:
SortedDictionary<string, SortedDictionary<string, int>> baseItemCounts = new SortedDictionary<string, SortedDictionary<string, int>>();
baseItemCounts.Add("1450", new SortedDictionary<string, int>());
baseItemCounts["1450"].Add("1450M", 10);
baseItemCounts["1450"].Add("1350M", 20);
baseItemCounts["1450"].Add("1250M", 30);
foreach (SortedDictionary<string, int> sd in baseItemCounts.Values)
{
foreach (int count in sd.Values)
{
Console.WriteLine("{0}", count);
}
}
SwDevMan81正确:baseItemCounts[1450][1450M]将返回15 如果要查看列表并返回排序后的值,请尝试以下操作:
foreach (string key1 in baseItemCounts.Keys)
{
foreach (string key2 in baseItemCounts[key1].Keys)
{
Console.WriteLine("{0}, {1}, {2}", key1, key2, baseItemCounts[key1][key2]);
}
}
记住按键进行迭代,以便从排序中获益
SortedDictionary<string, SortedDictionary<string, int>> baseItemCounts =
new SortedDictionary<string, SortedDictionary<string, int>>();
baseItemCounts.Add("1450", new SortedDictionary<string, int>());
baseItemCounts["1450"].Add("1450M", 15);
foreach (KeyValuePair<string, SortedDictionary<string, int>> kv in baseItemCounts)
{
Console.WriteLine(kv.Key);
foreach (KeyValuePair<string, int> x in kv.Value)
Console.WriteLine(x.Key + "==>" + x.Value);
}
那就可以了谢谢你的帮助,但我想用foreach打印出来。由于项目可能100+大家好,感谢您的支持我的问题已经解决。