简体   繁体   English

从字典中选择值作为字符串列表

[英]Select Values from Dictionary as List of Strings

I have a dictionary with following Structure 我有一本具有以下结构的字典

Dictionary<string, List<string>> 

I want to extract all the values of the above dictionary and pass as list of strings to another method. 我想提取上述字典的所有值,并将其作为字符串列表传递给另一种方法。

like MyMethod(List<string>) Do we have an efficient way to do this without foreach loop? MyMethod(List<string>) ,我们是否有一种有效的方法来执行此操作而无需foreach循环?

Assuming you mean you want all values as a flat list, you can use LINQ: 假设您想将所有值都作为一个平面列表,则可以使用LINQ:

var myDict = new Dictionary<string, List<string>>();
var allValues = myDict.Values.SelectMany(v => v).ToList();

.SelectMany will flatten the enumerable returned from the expression v => v . .SelectMany将展平从表达式v => v返回的可枚举。

If you're using .NET Core in Visual Studio, you might need to add using System.Linq; 如果您在Visual Studio中使用.NET Core,则可能需要using System.Linq;进行添加using System.Linq; to the top of the file. 到文件顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM