List<long> to comma delimited string in C#

This often comes up. I have a List and I want to go to a comma delimited string of all the elements in the list that I can use in SQL.

What is the most elegant way to do this in C#? Iterating over all of them is fine, except either the first or last element has to be special-cased since I don't want leading or trailing commas.

Got a good one-liner?

Answers


string.Join is your friend...

var list = new List<long> {1, 2, 3, 4};
var commaSeparated = string.Join(",", list);

Need Your Help

Pylons Custom Middleware return 404

python pylons wsgi paster

I have the following code as a middleware in an pylons application:

Is it possible to create a synchronnous response with CXF server and Camel which processes the request?

rest cxf apache-camel

I would like to have a REST web service using Apache CXF and send incomming requests to a camel:route for processing. Is it possible to create a response synchronnously (if a error during the proce...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.