Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C# Dictionary to multidimensional array

I am trying to convert Dictionary<string, string> to multidimensional array. The items inside multidimentional array should be in quotes. So far I get this ["[mary, online]","[alex, offline]"]. But the output should be something like [["mary", "online"], ["alex", "offline"]]. Please help.

public void sendUpdatedUserlist(IDictionary<string, string> message)
{
    string[] array = message
      .Select(item => string.Format("[{0}, {1}]", item.Key, item.Value))
     .ToArray();
}
like image 606
Alex Avatar asked Oct 28 '25 02:10

Alex


1 Answers

If you insist on 2d array (string[,], not jagged string[][]) you can create the array and fill it row by row:

string[,] array = new string[message.Count, 2];

int index = 0;

foreach (var pair in message) {
  array[index, 0] = pair.Key;
  array[index++, 1] = pair.Value; 
}
like image 159
Dmitry Bychenko Avatar answered Oct 29 '25 18:10

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!