Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat all strings inside a List<string> using LINQ

Tags:

c#

linq

.net-3.5

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.

like image 612
Jobi Joy Avatar asked Feb 18 '09 00:02

Jobi Joy


People also ask

How do I concatenate a list of strings?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

How do you concatenate in Linq?

In LINQ, the concatenation operation contains only one operator that is known as Concat. It is used to append two same types of sequences or collections and return a new sequence or collection. It does not support query syntax in C# and VB.NET languages. It support method syntax in both C# and VB.NET languages.

What is the best way to concatenate strings in C#?

The simplest method of adding two strings in C# is using + or += operators.

How use all in Linq C#?

The Linq All Operator in C# is used to check whether all the elements of a data source satisfy a given condition or not. If all the elements satisfy the condition, then it returns true else return false. There is no overloaded version is available for the All method.


1 Answers

String.Join(delimiter, list); 

is sufficient.

like image 75
Sedat Kapanoglu Avatar answered Sep 19 '22 05:09

Sedat Kapanoglu