且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

转换列表<&诠释GT;到整数的加入字符串?

更新时间:2022-11-22 13:20:53

  INT []列表=新[] {3,99,6}; 
字符串s =的string.join(,,list.Select(X =&GT; x.ToString())ToArray的());

修改,C#4.0



使用C#4.0中,有 的另一个重载的string.join ,最终使传递一个的IEnumerable&LT;串&GT; 的IEnumerable&LT; T&GT; 直接。没有必要创建一个数组,而且也没有必要要求的ToString(),这是隐式调用:

 字符串s =的string.join(,清单); 

通过明确的格式化字符串:

 字符串s =的string.join(,list.Select(X =&GT; x.ToString(/*...*/)); 


I have an int array with the value 3,99,6. How do i convert the array into the string 3,99,6 with linq?

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 overload of string.Join, which finally allows passing an IEnumerable<string> or IEnumerable<T> directly. There is no need to create an Array, and there is also no need to call ToString(), which is called implicitly:

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

With explicit formatting to string:

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