I tried to append the items in a List<string>
to a StringBuilder
with LINQ:
items.Select(i => sb.Append(i + ","));
I fou开发者_运维百科nd 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 a neat way of doing this in a one-liner?
items.ForEach(item => sb.Append(item + ","));
You could use a simple foreach
loop. That way you have statements which modify the StringBuilder, instead of using an expression with side-effects.
And perhaps your problem is better solved with String.Join(",", items)
.
Try this:
String [] items={"Jan","Feb","Mar"};
StringBuilder sb=new StringBuilder();
foreach(var entity in items)
{
sb.Append(entity + Environment.NewLine);
}
Textbox1.Text=sb.tostring();
Output:
Jan
Feb
Mar
精彩评论