How to unifiy two arrays in a dictionary?

后端 未结 4 1238
说谎
说谎 2020-12-25 13:09

If you have two arrays string[] a and int[] b how can you get a Dictionary from it most efficiently and with least c

4条回答
  •  臣服心动
    2020-12-25 13:41

    If this is .Net 4, then you can do the following:

    var result = a.Zip(b, (first, second) => new {first, second})
        .ToDictionary(val => val.first, val => val.second);
    

    Without Zip, you can also do this:

    var result = Enumerable.Range(0, a.Length).ToDictionary(i => a[i], i => b[i]);
    

提交回复
热议问题