ChatGPT解决这个技术问题 Extra ChatGPT

Concat all strings inside a List<string> using LINQ

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.

Why linq and not string.Join() ?
string.Join is better but I think linq makes your code fun, that could be the why!
String.Join is better because it uses a StringBuilder and avoids the inherrent O(n^2) performance of repeated concatenation.
performance issues using LINQ ?

S
Sedat Kapanoglu
string result = String.Join(delimiter, list);

is sufficient.


I am all for LINQ solutions but this is more efficient than LINQ and the Aggregate() method.
much cleaner! worked great for me! string.Join(", ", accs.Select(x => x.AccountID).ToArray()),
@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.
This can also be used for things other than List<String>s and will call the ToString() method.
@alansiqueira27 well, a List<string> 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.
C
Caius Jard

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)

O(n^2) time strikes again.
If you can't see the Aggregate method, you need to add using System.Linq;
Problem is that above LinQ method does not work with empty or single-element list.
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.
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!
C
Caius Jard

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.Join(delimiter, list.ToArray());

And this is for a list of custom objects:

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

String.Join has an overload that takes an IEnumerable, so you don't need the ToArray() call
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().
Ah! That last overload was the one I was looking for. I knew there had to be a way to extract a specific property. :)
d
dev.bv
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));

J
Jacob Proffitt

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.


Why to do you have to call .ToArray() ?
Because back in the bad ole days of 2009, string.Join didn't have an extension that accepted an IEnumerable.
P
Peter Mortensen

You can simply use:

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

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

Happy coding!


J
Jordão

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(", ");

P
Peter McG
List<string> strings = new List<string>() { "ABC", "DEF", "GHI" };
string s = strings.Aggregate((a, b) => a + ',' + b);

P
Peter Mortensen

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

W
Welcor

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);
    }
}

R
Robrecht Dewaele

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.

The currently accepted answer does not seem to work with empty or singleton sequences. It also suffers from a performance issue.

The currently most upvoted answer does not explicitly address the generic string conversion requirement, when ToString does not yield the desired result. (This can be remedied by adding a call to Select.)

Another answer includes a note that may lead some to believe that the performance issue is inherent to LINQ. ("Using LINQ to turn enumerables into delimited strings can cause serious performance problems.")

I noticed this comment about sending the query to the database.

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.


R
Reza Jenabi

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