Take the 2-minute tour ×

I have a Dictionary<int, string> which I want to take the Key collection into a CSV string.

I planned to do:

String.Join(",", myDic.Keys.ToArray().Cast<string[]>());

The cast is failing though.


share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

How about this...

String.Join(",", myDic.Keys.Select(o=>o.ToString()).ToArray());
share|improve this answer
add comment

This will work:

String.Join(",", myDic.Keys.Select(i => i.ToString()).ToArray());
share|improve this answer
add comment

Cast to a string, not a string[]

String.Join(",", myDic.Keys.ToArray().Cast<string>());

Edit: This does not work - Cast does not perform type conversion. There is a ConvertAll method on Array which is just for this purpose:

String.Join(",", Array.ConvertAll(myDic.Keys.ToArray(), i => i.ToString());
share|improve this answer
add comment

Your Answer


