Convert a C# string array to a dictionary

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

    var dict = a.Select((s, i) => new { s, i })
                .GroupBy(x => x.i / 2)
                .ToDictionary(g => g.First().s, g => g.Last().s);
    

提交回复
热议问题