LINQ:我如何串联整数列表为逗号分隔字符串? [英] LINQ: How do I concatenate a list of integers into comma delimited string?
问题描述
这可能是一些愚蠢的我错过了,但我尽量串联整数列表,而不是与它们求和的:
It's probably something silly I missed, but I try to concatenate a list of integers instead of summing them with:
integerArray.Aggregate((accumulator, piece) => accumulator+"," + piece)
编译器抱怨有关参数错误。有一个漂亮的方式做到这一点,而无需经过一个循环?
The compiler complained about argument error. Is there a slick way to do this without having to go through a loop?
推荐答案
.NET的哪个版本?在4.0中,您可以使用的string.join(,integerArray)
。在3.5我会被诱惑只是使用的string.join(,Array.ConvertAll(integerArray,我= GT; i.ToString()));
(假设它的是的数组)。否则,无论是的做的一个数组,或者使用的StringBuilder
。
Which version of .NET? In 4.0 you can use string.Join(",",integerArray)
. In 3.5 I would be tempted to just use string.Join(",",Array.ConvertAll(integerArray,i=>i.ToString()));
(assuming it is an array). Otherwise, either make it an array, or use StringBuilder
.
这篇关于LINQ:我如何串联整数列表为逗号分隔字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!