连接字典中的键和值<;字符串,<;IEnumerable字符串>>;列出一个清单

本文关键字:gt 字符串 lt 一个 IEnumerable 字典 键和值 连接 | 更新日期: 2023-09-27 18:25:30

我有

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);

edit:我想把它作为一个LINQ表达式来做,而不是循环

连接字典中的键和值<;字符串,<;IEnumerable字符串>>;列出一个清单

有多种方法可以对此进行蒙皮。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);