If you insist on doing this LINQy way:
StringBuilder builder = StringArray.Aggregate( new StringBuilder(), (sb, s) => sb.AppendLine(s) );
Alternatively, as Luke pointed out in a comment on another post, you could say
Array.ForEach(StringArray, s => stringBuilder.AppendLine(s));
The reason Select doesn't work is because Select designed to project and create an IEnumerable projection. So the line of code
StringArray.Select(s => stringBuilder.AppendLine(s))
does not StringArray through a StringArray call that calls stringBuilder.AppendLine(s) at each iteration. Rather, it creates an IEnumerable<StringBuilder> that can be enumerated.
I guess you could say
var e = stringArray.Select(x => stringBuilder.AppendLine(x)); StringBuilder sb = e.Last(); Console.WriteLine(sb.ToString());
but it is really disgusting.
jason
source share