如何使用 C# 遍历 Dictionary<string, object>
How to iterate through Dictionary<string, object> using c#
我有一个
Dictionary<string, List<string>> demo = new Dictionary<string, List<string>>();
List<String> key1val= new List<string>();
key1val.Add("1");
key1val.Add("2");
key1val.Add("3");
List<String> key2val= new List<string>();
key2val.Add("4");
key2val.Add("5");
demo.Add("key1", key1val);
demo.Add("key2", key2val);
foreach (var item in demo)
{
foreach(var it in item.Value)
{
Console.WriteLine(it);
}
}
我想遍历所有键,例如 key1,key2
。
如果当前项是 key1
,那么内部循环应该获取关于 key1
的所有值并将其迭代到第二个循环
loop
iterate though key1
iterate though [1,2,3]
loop
iterate though key2
iterate though [4,5]
foreach (var item in demo )
{
WriteLine("loop");
WriteLine($"iterate though {item.key}");
WriteLine($"iterate though {item.Value}");
WriteLine();
}
这应该输出你想要的
但是,您可以:
foreach (var item in demo )
{
WriteLine("loop");
WriteLine($"iterate though {item.key}");
if (item.Value is IEnumerable<string> val) foreach(var v in val) WriteLine(v);
WriteLine();
}
嗯,使用 2 个嵌套循环。
外层遍历键,内层遍历相应条目的值:
Dictionary<string, List<string>> demo = new Dictionary<string, List<string>>();
List<String> key1val= new List<string>();
key1val.Add("1");
key1val.Add("2");
key1val.Add("3");
List<String> key2val= new List<string>();
key2val.Add("4");
key2val.Add("5");
demo.Add("key1", key1val);
demo.Add("key2", key2val);
foreach (var key in demo.Keys)
{
Console.WriteLine(key);
foreach (var elem in demo[key])
{
Console.WriteLine(elem);
}
}
这输出
key1
1
2
3
key2
4
5
使用 LINQ SelectMany
将值累积到单个列表中:
foreach (string value in demo.SelectMany(kvp => kvp.Value))
{
Console.WriteLine(val);
}
foreach (var item in demo)
{
Console.WriteLine("loop");
Console.WriteLine($"iterate though {item.Key}");
var valueAsString = $"[{String.Join(",", item.Value)}]";
Console.WriteLine($"iterate though {valueAsString}");
}
输出:
loop
iterate though key1
iterate though [1,2,3]
loop
iterate though key2
iterate though [4,5]
Linq 解决方案包括 Concat
将 Key
和 Values
组合成一个 单个 集合SelectMany
扁平化 他们:
Dictionary<string, List<string>> demo = ...
var result = demo
.SelectMany(pair => new string[] { pair.Key }.Concat(pair.Value));
// Let's have a look
foreach (var item in result)
Console.WriteLine(item);
我有一个
Dictionary<string, List<string>> demo = new Dictionary<string, List<string>>();
List<String> key1val= new List<string>();
key1val.Add("1");
key1val.Add("2");
key1val.Add("3");
List<String> key2val= new List<string>();
key2val.Add("4");
key2val.Add("5");
demo.Add("key1", key1val);
demo.Add("key2", key2val);
foreach (var item in demo)
{
foreach(var it in item.Value)
{
Console.WriteLine(it);
}
}
我想遍历所有键,例如 key1,key2
。
如果当前项是 key1
,那么内部循环应该获取关于 key1
的所有值并将其迭代到第二个循环
loop
iterate though key1
iterate though [1,2,3]
loop
iterate though key2
iterate though [4,5]
foreach (var item in demo )
{
WriteLine("loop");
WriteLine($"iterate though {item.key}");
WriteLine($"iterate though {item.Value}");
WriteLine();
}
这应该输出你想要的 但是,您可以:
foreach (var item in demo )
{
WriteLine("loop");
WriteLine($"iterate though {item.key}");
if (item.Value is IEnumerable<string> val) foreach(var v in val) WriteLine(v);
WriteLine();
}
嗯,使用 2 个嵌套循环。
外层遍历键,内层遍历相应条目的值:
Dictionary<string, List<string>> demo = new Dictionary<string, List<string>>();
List<String> key1val= new List<string>();
key1val.Add("1");
key1val.Add("2");
key1val.Add("3");
List<String> key2val= new List<string>();
key2val.Add("4");
key2val.Add("5");
demo.Add("key1", key1val);
demo.Add("key2", key2val);
foreach (var key in demo.Keys)
{
Console.WriteLine(key);
foreach (var elem in demo[key])
{
Console.WriteLine(elem);
}
}
这输出
key1
1
2
3
key2
4
5
使用 LINQ SelectMany
将值累积到单个列表中:
foreach (string value in demo.SelectMany(kvp => kvp.Value))
{
Console.WriteLine(val);
}
foreach (var item in demo)
{
Console.WriteLine("loop");
Console.WriteLine($"iterate though {item.Key}");
var valueAsString = $"[{String.Join(",", item.Value)}]";
Console.WriteLine($"iterate though {valueAsString}");
}
输出:
loop
iterate though key1
iterate though [1,2,3]
loop
iterate though key2
iterate though [4,5]
Linq 解决方案包括 Concat
将 Key
和 Values
组合成一个 单个 集合SelectMany
扁平化 他们:
Dictionary<string, List<string>> demo = ...
var result = demo
.SelectMany(pair => new string[] { pair.Key }.Concat(pair.Value));
// Let's have a look
foreach (var item in result)
Console.WriteLine(item);