我想遍历包含字典里面的arraylist。如何获取foreach循环中arraylist内的字典的键和值
foreach(Dictionary<string, string> tempDic in rootNode)
{
Response.Write(tempDic.key + "," tempDic.value + "<br>");
}
如何访问字典键和值?
我想遍历包含字典里面的arraylist。如何获取foreach循环中arraylist内的字典的键和值
foreach(Dictionary<string, string> tempDic in rootNode)
{
Response.Write(tempDic.key + "," tempDic.value + "<br>");
}
如何访问字典键和值?
您还需要在字典中循环,为了做到这一点,您可以使用Foreach
来迭代tempDic
。
foreach(Dictionary<string, string> tempDic in rootNode)
{
foreach(KeyValuePair<string, string> _x in tempDic)
{
Response.Write(_x.key + "," + _x.value + "<br>");
}
}
但数组列表只有12行,有哪行只有一个字典,而使用2个foreach会打印出大约62行数据。 – hkguile
你可以使用LINQ第一个获得所有KeyValuePair
s的列表(实际上IEnumerable<KeyValuePair<string, string>>
):
var pairs = rootNode.OfType<Dictionary<string, string>>()
.SelectMany(d => d.AsEnumerable());
foreach (KeyValuePair<string, string> tempPair in pairs)
{
Response.Write(tempPair.Key + "," + tempPair.Value + "<br>");
}
所以,这将是足够的做只有一个foreach循环。另一个将由LINQ为您完成。
你是什么意思“字典键和值?”字典有多个键和值。 – StriplingWarrior