我有
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
我可以将其全部合二为一,但我怎样才能将密钥添加到每个?
var SL_requirements = SL_requirementsDict.SelectMany(kvp => kvp.Value);
编辑:我想将它作为LINQ表达式而不是循环
答案 0 :(得分:9)
有各种剥皮方式。 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);