将List项附加到StringBuilder [英] Append List items to StringBuilder
本文介绍了将List项附加到StringBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用Linq将 List< string>
中的项目附加到 StringBuilder
:
I tried to append the items in a List<string>
to a StringBuilder
with Linq:
items.Select(i => sb.Append(i + ","));
发现类似问题这里解释了为什么以上不起作用,但我找不到每个
ForEach
或 List
上的任何类似内容我可以使用。
A found a similar question here which explains why the above doesn't work, but I couldn't find an Each
of ForEach
or anything similar on List
which I could use instead.
在一个班轮里有没有任何巧妙的方法呢?
Is there any neat way of doing this in a one liner?
推荐答案
怎么样使用简单的 foreach
循环。这样你就有了修饰StringBuilder的语句,而不是使用带副作用的表达式。
How about using a simple foreach
loop. That way you have statements which modify the StringBuilder, instead of using an expression with side-effects.
也许用 String.Join(,,item)
这篇关于将List项附加到StringBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文