5

我有

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 表达式而不是循环

4

1 回答 1

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);
于 2013-07-29T10:09:01.793 回答