Programmerzzz
Programmerzzz

Reputation: 1287

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?

Upvotes: 0

Views: 433

Answers (1)

ProgrammingLlama
ProgrammingLlama

Reputation: 38767

Assuming you mean you want all values as a flat list, you can use 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.

If you're using .NET Core in Visual Studio, you might need to add using System.Linq; to the top of the file.

Upvotes: 3

Related Questions