Convert list <int> to concatenated string ints?

I have an int array with a value of 3.99.6. How to convert an array to a string 3,99,6 with linq?

+14
arrays c # linq
Jul 09 2018-10-09T00:
source share
2 answers
 int[] list = new [] {3, 99, 6}; string s = string.Join(",", list.Select(x => x.ToString()).ToArray()); 

Edit, C # 4.0

With C # 4.0, there is another string.Join overload that finally allows you to directly pass an IEnumerable<string> or IEnumerable<T> . There is no need to create an array, and there is no need to call ToString() , which is called implicitly:

 string s = string.Join(",", list); 

With explicit formatting to a string:

 string s = string.Join(",", list.Select(x => x.ToString(/*...*/)); 
+21
Jul 09 '10 at 9:40
source share
— -

Stefan's solution is correct and pretty much required for .NET 3.5. In .NET 4, there is a String.Join overload that accepts an IEnumerable<string> , so you can use:

 string s = string.Join(",", list.Select(x => x.ToString()); 

or even just:

 string s = string.Join(",", list); 
+12
Jul 09 2018-10-09T00:
source share



All Articles