What is the most efficient way to write the old-school:
StringBuilder sb = new StringBuilder();
if (strings.Count > 0)
{
foreach (string s in strings)
{
sb.Append(s + ", ");
}
sb.Remove(sb.Length - 2, 2);
}
return sb.ToString();
...in LINQ?
There are various alternative answers at this previous question - which admittedly was targeting an integer array as the source, but received generalised answers.
Lots of choices here. You can use LINQ and a StringBuilder so you get the performance too like so:
You can use
StringBuilder
inAggregate
:(The
Select
is in there just to show you can do more LINQ stuff.)Real example from my code:
A query is an object that has a Name property which is a string, and I want the names of all the queries on the selected list, separated by commas.
By 'super-cool LINQ way' you might be talking about the way that LINQ makes functional programming a lot more palatable with the use of extension methods. I mean, the syntactic sugar that allows functions to be chained in a visually linear way (one after the other) instead of nesting (one inside the other). For example:
can be written like this:
You can see how the second example is easier to read. You can also see how more functions can be added with less of the indentation problems or the Lispy closing parens appearing at the end of the expression.
A lot of the other answers state that the
String.Join
is the way to go because it is the fastest or simplest to read. But if you take my interpretation of 'super-cool LINQ way' then the answer is to useString.Join
but have it wrapped in a LINQ style extension method that will allow you to chain your functions in a visually pleasing way. So if you want to writesa.Concatenate(", ")
you just need to create something like this:This will provide code that is as performant as the direct call (at least in terms of algorithm complexity) and in some cases may make the code more readable (depending on the context) especially if other code in the block is using the chained function style.
This answer shows usage of LINQ (
Aggregate
) as requested in the question and is not intended for everyday use. Because this does not use aStringBuilder
it will have horrible performance for very long sequences. For regular code useString.Join
as shown in the other answerUse aggregate queries like this:
This outputs:
An aggregate is a function that takes a collection of values and returns a scalar value. Examples from T-SQL include min, max, and sum. Both VB and C# have support for aggregates. Both VB and C# support aggregates as extension methods. Using the dot-notation, one simply calls a method on an IEnumerable object.
Remember that aggregate queries are executed immediately.
More information - MSDN: Aggregate Queries
If you really want to use
Aggregate
use variant usingStringBuilder
proposed in comment by CodeMonkeyKing which would be about the same code as regularString.Join
including good performance for large number of objects: