How to convert a multi-dimensional array to a dictionary?

  • A+

I have a n-by-3 array I wish to convert to a Dictionary<string,string[]> where the first column is the key and the rest of the column as an array for the value.

For example, Key = arr[0,0], Value = new string[2] {arr[0,1], arr[0,2]}.

I'm aware of ToDictionary but I don't know how to set the value part!

arr.ToDictionary(x=>arr[x,0],x=>new string[2]{arr[x,1],arr[x,2]}); //This doesn't work!!! 

Could somebody teach me how to set it up correctly, please!?


Sometimes not using linq is easier to read and faster:

 var dict = new Dictionary<string, string[]>();  for (int i = 0; i < arr.GetLength(0); i++)       dict[arr[i, 0]] = new string[] { arr[i, 1], arr[i, 2] }; 

But when you feel like you REALLY need to use linq:

 Enumerable.Range(0, arr.GetLength(0))      .ToDictionary(i => arr[i, 0], i => new string[] {arr[i, 1], arr[i, 2]}); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: