用两个数组填充字典。我不知道如何使用foreach从中获取数据列表。例如:17,16 ....请告诉我该怎么做?
Reading a properties file using ${Key} expression
Reading a properties file using ![p[‘Key’]] expression
Reading a properties file using p() function from DataWeave
答案 0 :(得分:1)
从您的示例中,您似乎正在寻找Dictionary<string,int>
,对于每个索引,您希望密钥是words
中的第i个项,并且值为i& #39; times
中的项目。
foreach
方法将是:
var dict = new Dictionary<string,int>();
for(int i = 0; i < words.Lenth; i++)
{
dict.Add(words[i], times[i];
}
请注意,这假设times
与words
的次数至少相同,如果不是,则会产生IndexOutOfRangeException
。
linq方法将是:
// C# 7.0
var dict = words.Zip(times, (w,t) => (w,t)).ToDictionary(key => key.w, value => value.t);
// C# prior to 7.0
var dict = words.Zip(times, (w,t) => new { w,t })
.ToDictionary(key => key.w, value => value.t);
请注意,Zip
将返回具有较低计数的集合的项目。