Vik*_*ren 5 c# linq dictionary
我有
Dictionary<string,IEnumerable<string>> pathAndItems = new Dictionary<string,IEnumerable<String>>();
Run Code Online (Sandbox Code Playgroud)
如
this/is/path/: {hey, ho, lets, go}
another/path/: {hey, hello}
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用所有连接的值制作一个IEnumerable.
this/is/path/hey, this/is/path/ho, this/is/path/lets, this/is/path/go, another/path/hey, another/path/hello
Run Code Online (Sandbox Code Playgroud)
我可以把所有这些都集中在一起,但我怎样才能将密钥添加到每个密钥中?
var SL_requirements = SL_requirementsDict.SelectMany(kvp => kvp.Value);
Run Code Online (Sandbox Code Playgroud)
编辑:我想将它作为LINQ表达式而不是循环
有各种剥皮方法.SelectMany还允许您(source, projected-element)在查询表达式中指定对每个对执行的操作:
var query = from pair in dictionary
from value in pair.Value
select pair.Key + "/" + value;
Run Code Online (Sandbox Code Playgroud)
或者用点符号表示:
var query = dictionary.SelectMany(kvp => kvp.Value,
(kvp, value) => kvp.Key + "/" + value);
Run Code Online (Sandbox Code Playgroud)