What is the functional programming approach to convert an IEnumerable<string>
to a delimited string? I know I can use a loop, but I'm trying to wrap my head around functional programming.
Here's my example:
var selectedValues = from ListItem item in checkboxList.Items where item.Selected select item.Value; var delimitedString = ??
.. or could I do this in just the first var assignment (append each result to the previous)?
string.Join(", ", string[] enumerable)
Here's an example with a StringBuilder
. The nice thing is that Append()
returns the StringBuilder
instance itself.
return list.Aggregate( new StringBuilder(), ( sb, s ) => ( sb.Length == 0 ? sb : sb.Append( ',' ) ).Append( s ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With