我有
Dictionary<string,IEnumerable<string>> pathAndItems = new Dictionary<string,IEnumerable<String>>();
比如
this/is/path/: {hey, ho, lets, go}
another/path/: {hey, hello}
我想做的是创建一个包含所有串联值的 IEnumerable。
this/is/path/hey, this/is/path/ho, this/is/path/lets, this/is/path/go, another/path/hey, another/path/hello
我可以合而为一,但如何为每个添加 key ?
var SL_requirements = SL_requirementsDict.SelectMany(kvp => kvp.Value);
编辑:我想用 LINQ 表达式而不是循环来做
最佳答案
有多种方法可以对此进行蒙皮。 SelectMany
允许您指定如何处理每个 (source, projected-element)
对,无论是否在查询表达式中:
var query = from pair in dictionary
from value in pair.Value
select pair.Key + "/" + value;
或点符号:
var query = dictionary.SelectMany(kvp => kvp.Value,
(kvp, value) => kvp.Key + "/" + value);
关于c# - 连接 dictionary<string,<IEnumerable string>> 中的键和值以生成一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921162/