拼合使用LINQ列出的C#字典 [英] Flatten a C# Dictionary of Lists with Linq
本文介绍了拼合使用LINQ列出的C#字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典在C#中:
I have a Dictionary in C#:
Dictionary<string, List<string>>
我如何使用LINQ拼合为一本列表<串>
包含所有的词典列表的?
How can I use Linq to flatten this into one List<string>
that contains all of the lists in the Dictionary?
谢谢!
推荐答案
很容易:
var list = dictionary.Values // To get just the List<string>s
.SelectMany(x => x) // Flatten
.ToList(); // Listify
下面的的SelectMany
调用需要在这种情况下的列表的元素 - 的输入和项目的每个单输入到输出另一序列(其使字典的值的列表)的序列。然后,它变平序列该序列到单个序列
Here the SelectMany
call takes a sequence of inputs (the lists which make the values of the dictionary) and projects each single input into another sequence of outputs - in this case "the elements of the list". It then flattens that sequence of sequences into a single sequence.
这篇关于拼合使用LINQ列出的C#字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文