Take the 2-minute tour ×

What is the functional programming approach to convert an IEnumerable 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)?

share|improve this question
add comment

8 Answers

up vote 12 down vote accepted
var delimitedString = selectedValues.Aggregate((x,y) => x + ", " + y);
share|improve this answer
That will be very expensive in terms of intermediate strings. Very FP, but not very practical. yoda.arachsys.com/csharp/stringbuilder.html –  Marc Gravell Oct 3 '08 at 14:41
@Marc, I answered the question asked, it was about FP, not about performance. –  Ilya Ryzhenkov Oct 5 '08 at 20:36
add comment
string.Join(", ", string[] enumerable)
share|improve this answer
+1, but I'd say string.Join(", ", selectedValues.ToArray()) –  Lucas Oct 4 '08 at 16:55
@Lucas, not if you're using .NET 4. msdn.microsoft.com/en-us/library/dd992421.aspx –  Omer Raviv Sep 9 '10 at 16:29
@Omer, true! but this was back in Oct 2008, before .NET 4 :) –  Lucas Sep 9 '10 at 17:43
or was it . . . –  Benjamin Apr 4 '13 at 1:34
add comment
var delimitedString = string.Join(",", checkboxList.Items.Where(i => i.Selected).Select(i => i.Value).ToArray());
share|improve this answer
add comment

Well, in this case the functional approach might not be best suited, simply because there isn't a LINQ "ForEach", and you don't want to use string concatenation: you want to use StringBuilder. You could use ToArray (an example just appeared above), but I'd be tempted to simply use:

    StringBuilder sb = new StringBuilder();
    foreach(ListViewItem item in checkboxList.SelectedItems) {
        if(sb.Length > 0) sb.Append(',');
    string s = sb.ToString();

Not functional programming, but it works... of course, if your source is already a string[] then string.Join is perfect. (LINQ is a great tool, but not necessarily always the best tool for every job)

share|improve this answer
add comment

Here's a LINQ/functional way of doing it.

string[] toDelimit = CallSomeFunction();
return toDelimit.Aggregate((x, y) => x + "," + y);
share|improve this answer
add comment

AviewAnew is the best answer, but if what you are looking for is learning how to think in functional, what you should do is use a fold operation (or aggregate as it is called in NET).

items.Aggregate((accum, elem) => accum + ", " + elem);
share|improve this answer
Is string.join better for performance reasons? I would say it is better from a readability stand-point. –  Jeremy Oct 3 '08 at 14:45
I'd say yes; .ToArray() [to get the string[]] will use doubling, so not many intermediate arrays - and then string.Join is essentially StringBuilder; however, string concatenation will have a new string each time, so telescoping in size. –  Marc Gravell Oct 3 '08 at 14:49
Agree, this is the kind of solution you would find in a functional programming language (such as haskell), but not here. But grasping foldr is one of the keys to master functional languages. –  Santiago Palladino Oct 3 '08 at 14:54
So, is this an example of why functional languages are not generally as performant? –  Jeremy Oct 3 '08 at 15:15
add comment

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 ) );
share|improve this answer
last line should probably be ( sb.Length == 0 ? sb.Append(s) : sb.Append( ',' ) ).Append( s ) ); –  Tormod Nov 13 '12 at 21:49
add comment

This is 3.5 compatible:

var selectedValues = String.Join(",", (from ListItem item in checkboxList.Items where item.Selected select item.Value).ToArray());
share|improve this answer
add comment

Your Answer


