我需要从嵌套的Dictionary IN C#中获取数据.我的词典是这样的:
static Dictionary<string, Dictionary<ulong, string>> allOffset =
new Dictionary<string, Dictionary<ulong, string>>();
Run Code Online (Sandbox Code Playgroud)
我需要获取完整字典的所有键/值,如下所示:
string->>ulong, string
Run Code Online (Sandbox Code Playgroud)
提前致谢.
LINQ答案(读取所有三元组):
var qry = from outer in allOffset
from inner in outer.Value
select new {OuterKey = outer.Key,InnerKey = inner.Key,inner.Value};
Run Code Online (Sandbox Code Playgroud)
或(直接获取字符串):
var qry = from outer in allOffset
from inner in outer.Value
select outer.Key + "->>" + inner.Key + ", " + inner.Value;
foreach(string s in qry) { // show them
Console.WriteLine(s);
}
Run Code Online (Sandbox Code Playgroud)
或者一线解决方案
allOffset.SelectMany(n => n.Value.Select(o => n.Key+"->>"+o.Key+","+ o.Value))
.ToList()
.ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)
迭代外部字典,每次迭代嵌套字典的成员,即
(未经测试的代码)
foreach(var key1 in dc.Keys)
{
Console.WriteLine(key1);
var value1 = dc[key1];
foreach(var key2 in value1.Keys)
{
Console.WriteLine(" {0}, {1}", key2, value1[key2]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11002 次 |
| 最近记录: |