c# - 连接 dictionary<string,<IEnumerable string>> 中的键和值以生成一个列表

标签 c# linq dictionary

我有

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/

相关文章:

c# - 从 C# 调用的 C++ 写入输出窗口

c# - 字符串是否存在检查 20k 次

Azure 流分析 : "Stream Analytics job has validation errors: The given key was not present in the dictionary."

c++ STL map::operator []在被删除的条目上完成

c# - 请求失败,HTTP 状态为 401 : Unauthorized - IIS Upgrade - WebService & Web Application

mysql - 将可能的重复标记为不同的

c# - 我们可以用 linq 让它更简洁吗?

c# - 从不同集合中删除另一个列表的对象列表

dictionary - range 或 map 返回什么?

c# - 对 WCF Web API 进行版本控制的最佳方式是什么?