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

in rails, how to titleize the string anytime user.name is called?

ruby-on-rails ruby-on-rails-3

I'm guessing this one is very simple but I'm just not sure how to google for it.