5

I'm trying to convert an IEnumerable<int> to a string in the format #,#,#,... I'm having a terrible time attempting to make a method of this. What is a quick and easy way to handle?

Thanks.

Channing
  • 129
  • 2
  • 12
  • What was the problem with what you've tried? Have you searched ["create comma separated string"](http://stackoverflow.com/q/4884050/284240)? – Tim Schmelter Nov 12 '13 at 20:38

2 Answers2

15

Use String.Join:

string result = string.Join(",", enumerable);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    This is only available starting at `.net 4` [check out 3.5](http://msdn.microsoft.com/en-us/library/System.String.Join(v=vs.90).aspx). – allonhadaya Nov 12 '13 at 20:43
  • 1
    @allonhadaya Correct. You'd need to add your own overload, or first convert the sequence into a string array, if you were on an older version. – Servy Nov 12 '13 at 20:45
2

Are you talking about something like:

string.Join(",", e.Select(i=>i.ToString()).ToArray());

i.e., concatenating an enumerable of ints (e in this case)?

Blindy
  • 65,249
  • 10
  • 91
  • 131