Convert a C# string array to a dictionary

前端 未结 7 1796
死守一世寂寞
死守一世寂寞 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:55

    How about this ?

        var q = a.Zip(a.Skip(1), (Key, Value) => new { Key, Value })
                 .Where((pair,index) => index % 2 == 0)
                 .ToDictionary(pair => pair.Key, pair => pair.Value);
    
    0 讨论(0)
提交回复
热议问题