我有一个string1, string2,...,stringn
和每个字符串的值是
string1 = "s1k1-s1k2-s1k3-....-s1kn | s1v1-s1v2-s1v3-....-s1vn";
string2 = "s2k1-s2k2-s2k3-....-s2kn | s2v1-s2v2-s2v3-....-s2vn";
.............
stringn = "snk1-snk2-snk3-....-snkn | snv1-snv2-snv3-....-snvn";
现在,我想Dictionary<string, string>()
用键和值填充如下。
var dict = new Dictionary<string, string>();
dict.Add(s1k1, s1v1);
dict.Add(s1k2, s1v2);
:
:
dict.Add(snkn, snvn);
下面是我的示例代码:
string string1 = "s1k1-s1k2-s1k3-s1kn|s1v1-s1v2-s1v3-s1vn";
string string2 = "s2k1-s2k2-s2k3-s2kn|s2v1-s2v2-s2v3-s2vn";
string stringn = "snk1-snk2-snk3-snkn|snv1-snv2-snv3-snvn";
var dict = new Dictionary<string, string>();
new List<string> { string1, string2, stringn }.ForEach(str =>
{
var strSplit = str.Split('|');
var strKeys = strSplit[0].Split('-');
var strValues = strSplit[1].Split('-');
strKeys.Zip(strValues, (key, value) => new { key, value }).ToList().ForEach(filed => dict.Add(filed.key, filed.value));
});
是否可以优化此代码?