Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - IEnumerable to delimited string

Tags:

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)?

like image 980
Jeremy Avatar asked Oct 03 '08 14:10

Jeremy


2 Answers

string.Join(", ", string[] enumerable) 
like image 93
Tom Ritter Avatar answered Sep 23 '22 14:09

Tom Ritter


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 ) ); 
like image 30
Danko Durbić Avatar answered Sep 24 '22 14:09

Danko Durbić