Cast Dictionary KeyCollection to String array
I have a Dictionary<int, string> which开发者_如何学C 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
How about this...
String.Join(",", myDic.Keys.Select(o=>o.ToString()).ToArray());
This will work:
String.Join(",", myDic.Keys.Select(i => i.ToString()).ToArray());
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());
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论