views:

1049

answers:

3

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.

Thanks

+8  A: 

How about this...

String.Join(",", myDic.Keys.Select(o=>o.ToString()).ToArray());
Jason Punyon
+1  A: 

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());
Gabe Moothart
+3  A: 

This will work:

String.Join(",", myDic.Keys.Select(i => i.ToString()).ToArray());
Jason