Convert a IList<int> collection to a comma separated list
Asked Answered
M

6

6

Any elegant ways of converting a IList collection to a string of comma separated id's?

"1,234,2,324,324,2"

Macaroon answered 7/10, 2009 at 0:54 Comment(1)
Isn't this close enough to your other question?Signory
J
16
    IList<int> list = new List<int>( new int[] { 1, 2, 3 } );
    Console.WriteLine(string.Join(",", list));
Johnsen answered 7/10, 2009 at 0:57 Comment(5)
Doesn't work. ForEach() is defined on List<T>, not IList<T>.Spathe
No need to call ToArray. The string.Join overload used operates on IEnumerable<string>.Colene
Yep, fixed. Can't recall why I had that in there 9 years ago.Johnsen
@EdS. That overload was added in .NET 4.0, which came out two weeks after you wrote this answer. There's also a generic overload now, so there's no need for the Select either, you can just pass in an IEnumerable<T> (in this case, of ints) and it'll get the string value of each one for you. (That overload was also added at the same time.)Excommunicate
@Servy: That makes sense.Johnsen
S
5

You can do:

// Given: IList<int> collection;

string commaSeparatedInts = string.Join(",",collection.Select(i => i.ToString()).ToArray());
Spathe answered 7/10, 2009 at 0:58 Comment(0)
H
3

This will do it

IList<int> strings = new List<int>(new int[] { 1,2,3,4 });
string[] myStrings = strings.Select(s => s.ToString()).ToArray();
string joined = string.Join(",", myStrings);

OR entirely with Linq

string aggr = strings.Select(s=> s.ToString()).Aggregate((agg, item) => agg + "," + item);
Housewife answered 7/10, 2009 at 1:1 Comment(0)
K
3
// list = IList<MyObject>

var strBuilder = new System.Text.StringBuilder();

foreach(var obj in list)
{
  strBuilder.Append(obj.ToString());
  strBuilder.Append(",");
}

strBuilder = strBuilder.SubString(0, strBuilder.Length -1);
return strBuilder.ToString();
Kielty answered 7/10, 2009 at 1:2 Comment(2)
A good choice, I've always found StringBuilder to be a much faster at concatenation. Especially useful if the size of the list is BIG!Inform
StringBuilder.SubString doesn't exist, what you want is StringBuilder.ToString(int startIndex, int length)Oestrogen
A
0
List<int> intList = new List<int>{1,234,2,324,324,2};
var str = intList.Select(i => i.ToString()).Aggregate( (i1,i2) => string.Format("{0},{1}",i1,i2));
Console.WriteLine(str);
Adlei answered 7/10, 2009 at 2:32 Comment(4)
It would be helpful if the person down voting an answer would leave a comment as to why it was down voted. This is a perfectly legitimate solution to the problem!Adlei
Yes, legitimate, just not the most simple way to do it. The ranking system is simply about getting the best answers to the top.Johnsen
I would imagine that an answer would get down voted only if it is either incorrect, inaccurate or lacks details. Most of the answers posted here using LINQ are not necessarily "simple"!Adlei
I agree with you Abhijeet. Voting up is for ranking. Voting down is for incorrect. I set you back to 0.Somerville
R
0

mstrickland has a good idea on using string builder because of its speed with larger lists. However, you can't set a stringbuilder as a string. Try this instead.

    var strBuilder = new StringBuilder();

    foreach (var obj in list)
    {
        strBuilder.Append(obj.ToString());
        strBuilder.Append(",");
    }

    return strBuilder.ToString(0, strBuilder.Length - 1); 
Reconstruct answered 13/3, 2010 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.