667

Is there any easy LINQ expression to concatenate my entire List<string> collection items to a single string with a delimiter character?

What if the collection is of custom objects instead of string? Imagine I need to concatenate on object.Name.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119

12 Answers12

1224
string result = String.Join(delimiter, list);

is sufficient.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • 104
    I am all for LINQ solutions but this is more efficient than LINQ and the Aggregate() method. – andleer Feb 20 '09 at 18:29
  • 22
    much cleaner! worked great for me! string.Join(", ", accs.Select(x => x.AccountID).ToArray()), – m4tt1mus Jul 08 '11 at 16:34
  • 36
    @KonstantinSalavatov I had posted my answer before OP had clarified that it had to be in LINQ. It is still perfectly valid for anyone who bumps into this answer while looking for a "not-necessarily-LINQ" solution on Google. Regarding this answer "not useful" in that context is unfair. – Sedat Kapanoglu Jun 01 '12 at 10:47
  • 7
    This can also be used for things other than List``s and will call the ToString() method. – Kian Jul 29 '13 at 12:14
  • doesn't work if this is a query going to the database. It only works on memory. – alansiqueira27 Jan 07 '19 at 19:16
  • 2
    @alansiqueira27 well, a `List` is never a query going to the database. that's an entirely different problem but you can always call `.ToList()` to a query and merge later. – Sedat Kapanoglu Jan 07 '19 at 20:39
  • He said "using linq" – Welcor Jul 04 '20 at 13:51
  • 1
    @Blechdose see previous comments. – Sedat Kapanoglu Feb 16 '21 at 23:31
  • 1
    You can always have a linq query produce a list, then call this solution here. Perfectly acceptable. This is what I used when I searched for LINQ + Join. – Jason P Sallinger Aug 06 '21 at 18:25
561

Warning - Serious Performance Issues

Though this answer does produce the desired result, it suffers from poor performance compared to other answers here. Be very careful about deciding to use it


By using LINQ, this should work;

string delimiter = ",";
List<string> items = new List<string>() { "foo", "boo", "john", "doe" };
Console.WriteLine(items.Aggregate((i, j) => i + delimiter + j));

class description:

public class Foo
{
    public string Boo { get; set; }
}

Usage:

class Program
{
    static void Main(string[] args)
    {
        string delimiter = ",";
        List<Foo> items = new List<Foo>() { new Foo { Boo = "ABC" }, new Foo { Boo = "DEF" },
            new Foo { Boo = "GHI" }, new Foo { Boo = "JKL" } };

        Console.WriteLine(items.Aggregate((i, j) => new Foo{Boo = (i.Boo + delimiter + j.Boo)}).Boo);
        Console.ReadKey();

    }
}

And here is my best :)

items.Select(i => i.Boo).Aggregate((i, j) => i + delimiter + j)
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Ali Ersöz
  • 15,860
  • 11
  • 50
  • 64
  • 49
    O(n^2) time strikes again. – Kennet Belenky Jun 21 '10 at 21:03
  • 2
    If you can't see the Aggregate method, you need to add using System.Linq; – Cédric Guillemette Jan 05 '13 at 21:05
  • better using String.Format like items.Select(i => i.Boo).Aggregate((i, j) => string.Format("{0} {1} {2}", i, delimiter, j)) – Jone Polvora Sep 12 '13 at 02:06
  • 7
    Problem is that above LinQ method does not work with empty or single-element list. – Alexander Nov 22 '13 at 12:28
  • 2
    it will throw an InvalidOperationException in case items is empty. – 2xMax Nov 12 '14 at 08:06
  • 13
    why not just use string.join? Please accept Sedat's answer so that anyone in a rush doesn't choose this solution when Sedat's is the better choice. – Skystrider May 13 '16 at 17:03
  • 8
    DO NOT USE THIS. This solution will immediately tank application performance on even a trivial set of strings. Use Sedat's answer with string.Join! – Bryan Boettcher Mar 27 '17 at 20:32
  • 2
    @PreguntonCojoneroCabrón: no (and I'm not going to go create them), but I was on the team responsible for performance improvements to an application and this was a big hot-button item, responsible for seconds of page-load performance with a modest (10k items) list. – Bryan Boettcher Apr 27 '17 at 18:44
  • When you have a small list of strings that could be null, then this is the only solution that can be used: items?.Select(i => i.Boo)?.Aggregate((i, j) => i + delimiter + j) ?? string.empty – Pow-Ian Feb 17 '23 at 18:33
148

Note: This answer does not use LINQ to generate the concatenated string. Using LINQ to turn enumerables into delimited strings can cause serious performance problems

Modern .NET (since .NET 4)

This is for an array, list or any type that implements IEnumerable:

string.Join(delimiter, enumerable);

And this is for an enumerable of custom objects:

string.Join(delimiter, enumerable.Select(i => i.Boo));

Old .NET (before .NET 4)

This is for a string array:

string.Join(delimiter, array);

This is for a List<string>:

string.Join(delimiter, list.ToArray());

And this is for a list of custom objects:

string.Join(delimiter, list.Select(i => i.Boo).ToArray());
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
  • 30
    String.Join has an overload that takes an IEnumerable, so you don't need the ToArray() call – arolson101 Nov 14 '11 at 19:14
  • 12
    Keep in mind the IEnumerable overload only exists in 4.0 or later. If you're using an older version you will still need ToArray(). – Rakuen42 May 22 '12 at 20:00
  • 2
    Ah! That last overload was the one I was looking for. I knew there had to be a way to extract a specific property. :) – Mike Devenney Dec 30 '16 at 15:53
60
using System.Linq;

public class Person
{
  string FirstName { get; set; }
  string LastName { get; set; }
}

List<Person> persons = new List<Person>();

string listOfPersons = string.Join(",", persons.Select(p => p.FirstName));
dev.bv
  • 980
  • 9
  • 16
27

Good question. I've been using

List<string> myStrings = new List<string>{ "ours", "mine", "yours"};
string joinedString = string.Join(", ", myStrings.ToArray());

It's not LINQ, but it works.

Jacob Proffitt
  • 12,664
  • 3
  • 41
  • 47
10

You can simply use:

List<string> items = new List<string>() { "foo", "boo", "john", "doe" };

Console.WriteLine(string.Join(",", items));

Happy coding!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nishanth Shaan
  • 1,422
  • 15
  • 18
8

I think that if you define the logic in an extension method the code will be much more readable:

public static class EnumerableExtensions { 
  public static string Join<T>(this IEnumerable<T> self, string separator) {  
    return String.Join(separator, self.Select(e => e.ToString()).ToArray()); 
  } 
} 

public class Person {  
  public string FirstName { get; set; }  
  public string LastName { get; set; }  
  public override string ToString() {
    return string.Format("{0} {1}", FirstName, LastName);
  }
}  

// ...

List<Person> people = new List<Person>();
// ...
string fullNames = people.Join(", ");
string lastNames = people.Select(p => p.LastName).Join(", ");
Jordão
  • 55,340
  • 13
  • 112
  • 144
7
List<string> strings = new List<string>() { "ABC", "DEF", "GHI" };
string s = strings.Aggregate((a, b) => a + ',' + b);
Peter McG
  • 18,857
  • 8
  • 45
  • 53
4

I have done this using LINQ:

var oCSP = (from P in db.Products select new { P.ProductName });

string joinedString = string.Join(",", oCSP.Select(p => p.ProductName));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Biddut
  • 418
  • 1
  • 6
  • 17
2

Put String.Join into an extension method. Here is the version I use, which is less verbose than Jordaos version.

  • returns empty string "" when list is empty. Aggregate would throw exception instead.
  • probably better performance than Aggregate
  • is easier to read when combined with other LINQ methods than a pure String.Join()

Usage

var myStrings = new List<string>() { "a", "b", "c" };
var joinedStrings = myStrings.Join(",");  // "a,b,c"

Extensionmethods class

public static class ExtensionMethods
{
    public static string Join(this IEnumerable<string> texts, string separator)
    {
        return String.Join(separator, texts);
    }
}
Welcor
  • 2,431
  • 21
  • 32
2

This answer aims to extend and improve some mentions of LINQ-based solutions. It is not an example of a "good" way to solve this per se. Just use string.Join as suggested when it fits your needs.

Context

This answer is prompted by the second part of the question (a generic approach) and some comments expressing a deep affinity for LINQ.

Given that there is no answer matching all these requirements, I propose an implementation that is based on LINQ, running in linear time, works with enumerations of arbitrary length, and supports generic conversions to string for the elements.

So, LINQ or bust? Okay.
static string Serialize<T>(IEnumerable<T> enumerable, char delim, Func<T, string> toString)
{
    return enumerable.Aggregate(
        new StringBuilder(),
        (sb, t) => sb.Append(toString(t)).Append(delim),
        sb =>
        {
            if (sb.Length > 0)
            {
                sb.Length--;
            }

            return sb.ToString();
        });
}

This implementation is more involved than many alternatives, predominantly because we need to manage the boundary conditions for the delimiter (separator) in our own code.

It should run in linear time, traversing the elements at most twice.

Once for generating all the strings to be appended in the first place, and zero to one time while generating the final result during the final ToString call. This is because the latter may be able to just return the buffer that happened to be large enough to contain all the appended strings from the get go, or it has to regenerate the full thing (unlikely), or something in between. See e.g. What is the Complexity of the StringBuilder.ToString() on SO for more information.

Final Words

Just use string.Join as suggested if it fits your needs, adding a Select when you need to massage the sequence first.

This answer's main intent is to illustrate that it is possible to keep the performance in check using LINQ. The result is (probably) too verbose to recommend, but it exists.

1

You can use Aggregate, to concatenate the strings into a single, character separated string but will throw an Invalid Operation Exception if the collection is empty.

You can use Aggregate function with a seed string.

var seed = string.Empty;
var seperator = ",";

var cars = new List<string>() { "Ford", "McLaren Senna", "Aston Martin Vanquish"};

var carAggregate = cars.Aggregate(seed,
                (partialPhrase, word) => $"{partialPhrase}{seperator}{word}").TrimStart(',');

you can use string.Join doesn’t care if you pass it an empty collection.

var seperator = ",";

var cars = new List<string>() { "Ford", "McLaren Senna", "Aston Martin Vanquish"};

var carJoin = string.Join(seperator, cars);

Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34