Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate dictionary keys separated by comma's

I'm looking for a better way to concatenate dictionary keys, this is what I'm doing now :

Dictionary<int, string> myList = new Dictionary<int, string>();

myList.Add(1, "value");
myList.Add(2, "value");
myList.Add(3, "value");
myList.Add(4, "value");

string choices = "";

foreach (int key in myList.Keys)
{
    choices += key + " ";
}

choices = "(" + choices.Trim().Replace(" ", ",") + ")"; // (1,2,3,4)

I'm sure there is a better way, with LINQ maybe ?

like image 307
Abdusalam Ben Haj Avatar asked Nov 28 '22 14:11

Abdusalam Ben Haj


1 Answers

string.Format("({0})", string.Join(",", myList.Keys));
like image 69
Tim Schmelter Avatar answered Dec 10 '22 14:12

Tim Schmelter