Concatenate list of strings in C# using LINQ

Concatenate list of string into single string using LINQ in c#

More often you will need a list of strings from a single string with a Split method, not so often you need to get a single string from an IEnumerable of strings, but still those requirements exist form time to time.

This does not look like a complex tasks, but still there are several ways to achieve this.

As a sample list of strings we'll use list of color names from System.ConsoleColor enumeration type

Enum.GetNames(typeof(ConsoleColor))
    

Doing it with the loop

This is the worst way of doing it but it works in all .NET framework versions, which is not something you should worry about because I don't thing that anyone out there is still using anything older than at least .NET framework 4.0, but in case you are this is the way to do it:

String colorNames = String.Empty;
            foreach (var colorName in Enum.GetNames(typeof(ConsoleColor)))
            {
                colorNames = String.Format("{0}{1};", colorNames, colorName);
            }
            colorNames = colorNames.Substring(0, colorNames.Length - 1);
    
Note

Result of the loop will be string which ends with the separator you use. If this makes any difference to you, you might have to do additional Substring at the end

Using String.Join method

Simple single line concatenation of String list can be done with String.Join as following

String colorNames = String.Join(";", Enum.GetNames(typeof(ConsoleColor)));
    

This applies to .NET framework 4.0 onwards. Older versions of .NET framework will not accept IEnumerable<String> as a parameter, so you need to do additional cast to an Array.

String colorNames = String.Join(";", Enum.GetNames(typeof(ConsoleColor)).ToArray());
    

If you check the output, you will see that it does not end with a delimiter as in prevous case, so no additional Substring is required.

Using LINQ

Finally there is LINQ. For this kind of String transformation, most suitable is Agregate method.

Nothing special about the way linq expression is written, but it gies you more flexibility in terms of doing additional things which might be rquired during string concatenation.

String colorNames = Enum.GetNames(typeof(ConsoleColor)).Aggregate((v1, v2) => String.Format("{0};{1}",v1,v2));
    

This is my ppreffered way of doing String list concatenation as it can be easily implemented of a larger, more complicated LINQ expression where you can do more stuff in a single or just few lines of code.

 

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article