0

Simple question that I can't seem to find an answer to:

In C#, how do you take a Dictionary< TKey, TValue[] > and make it an TValue[] array - get all values from the dictionary into a single array?

I've tried experimenting with LINQ but can't find a solution, probably easier than I think.

1 Answer 1

6

You can use SelectMany from LINQ:

 var array = dic.SelectMany(p => p.Value).ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That worked! I haven't used SelectMany, just Select LINQ.
if it is Dictionary, should that just be dic.Values.ToArray()?
@EthanLi: the result of your code will be TValue[][], not TValue[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.