Creating a Comma Separated List from Ilist<String> or Ienumerable<String>

Creating a comma separated list from IList string or IEnumerable string

.NET 4+

IList<string> strings = new List<string>{"1","2","testing"};
string joined = string.Join(",", strings);

Detail & Pre .Net 4.0 Solutions

IEnumerable<string> can be converted into a string array very easily with LINQ (.NET 3.5):

IEnumerable<string> strings = ...;
string[] array = strings.ToArray();

It's easy enough to write the equivalent helper method if you need to:

public static T[] ToArray(IEnumerable<T> source)
{
return new List<T>(source).ToArray();
}

Then call it like this:

IEnumerable<string> strings = ...;
string[] array = Helpers.ToArray(strings);

You can then call string.Join. Of course, you don't have to use a helper method:

// C# 3 and .NET 3.5 way:
string joined = string.Join(",", strings.ToArray());
// C# 2 and .NET 2.0 way:
string joined = string.Join(",", new List<string>(strings).ToArray());

The latter is a bit of a mouthful though :)

This is likely to be the simplest way to do it, and quite performant as well - there are other questions about exactly what the performance is like, including (but not limited to) this one.

As of .NET 4.0, there are more overloads available in string.Join, so you can actually just write:

string joined = string.Join(",", strings);

Much simpler :)

IEnumerable string to comma separated string

You can use String.Join:

string result = String.Join(", ", emails);

Convert a list into a comma-separated string

Enjoy!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

Convert a IList int collection to a comma separated list

    IList<int> list = new List<int>( new int[] { 1, 2, 3 } );
Console.WriteLine(string.Join(",", list));

How can I get a comma separated list of values from a List using LINQ?

This should work

var result = string.Join(",", phrases.Select(x => $"'{x.PhraseId}'"));

Additional Resources

String.Join Method

Concatenates the elements of a specified array or the members of a
collection, using the specified separator between each element or
member.

Enumerable.Select Method

Projects each element of a sequence into a new form.

$ - string interpolation (C# Reference)

The $ special character identifies a string literal as an interpolated
string. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.

How to convert IEnumerable string to one comma separated string?

using System;
using System.Collections.Generic;
using System.Linq;

class C
{
public static void Main()
{
var a = new []{
"First", "Second", "Third"
};

System.Console.Write(string.Join(",", a));

}
}

How to convert List string into String of Comma Separated Quotes from List

LINQ's Select() extension method allows to convert each item in a collection:

string variants_str = String.Join(",", variants.Select(s => "'" + s + "'")); 

Demo: https://dotnetfiddle.net/I37xr6

Convert List Combo into Comma-Separated String in c#

I think this would be very handy

var colorList = new List<string>() { "D410", "D430", "D440", "D420" };
string commaSeparated = string.Join(",", colorList);
Console.WriteLine(commaSeparated);

or try solution based on Linq

Console.WriteLine(colorList.Select(s => s + ",").Aggregate((s, q) => s + q).TrimEnd(','));

The output

D410,D430,D440,D420

Edit

string result = string.Join(",", colorList.Select(e => "'" + e + "'"));
Console.WriteLine(result);

will give you

'D410','D430','D440','D420'


Related Topics



Leave a reply



Submit