var keys = new List { "a", "b", "c", "d" };
var vals = new List { 1, 2, 3, 4 };
var dict = new Dictionary();
Как заполнить dict так, чтобы по ключу "a" хранилось значение 1, "b" - 2, "c" - 3, "d" - 4?
Варианты ответа:
1) (4) keys.Select((key, i) => { dict.Add(key, vals[i]); return key; }).ToArray();
2) (1) dict = vals.ToDictionary(val => keys[vals.IndexOf(val)]);
3) (3) dict = Enumerable.Range(0, 4).Select(i => new KeyValuePair(keys[i], vals[i]));
4) (5) dict = keys.Select((key, i) => new { k = key, v = vals[i] }).ToDictionary();
5) (2) dict = keys.ToDictionary(key => vals[keys.IndexOf(key)]);
6) (6) dict = keys.Zip(vals, (key, val) => new { k = key, v = val }).ToDictionary(e => e.k, e => e.v);
var keys = new List { "a", "b", "c", "d" };
var vals = new List { 1, 2, 3, 4 };
var dict = new Dictionary();
Как заполнить dict так, чтобы по ключу "a" хранилось значение 1, "b" - 2, "c" - 3, "d" - 4?
Варианты ответа:
1) (4) keys.Select((key, i) => { dict.Add(key, vals[i]); return key; }).ToArray();
2) (1) dict = vals.ToDictionary(val => keys[vals.IndexOf(val)]);
3) (3) dict = Enumerable.Range(0, 4).Select(i => new KeyValuePair(keys[i], vals[i]));
4) (5) dict = keys.Select((key, i) => new { k = key, v = vals[i] }).ToDictionary();
5) (2) dict = keys.ToDictionary(key => vals[keys.IndexOf(key)]);
6) (6) dict = keys.Zip(vals, (key, val) => new { k = key, v = val }).ToDictionary(e => e.k, e => e.v);