Convert a C# string array to a dictionary

前端 未结 7 1858
死守一世寂寞
死守一世寂寞 2021-01-07 06:20

Is there an elegant way of converting this string array:

string[] a = new[] {\"name\", \"Fred\", \"colour\", \"green\", \"sport\", \"tennis\"};
7条回答
  •  佛祖请我去吃肉
    2021-01-07 06:35

    a.Select((input, index) = >new {index})
      .Where(x=>x.index%2!=0)
      .ToDictionary(x => a[x.index], x => a[x.index+1])
    

    I would recommend using a for loop but I have answered as requested by you.. This is by no means neater/cleaner..

提交回复
热议问题