我想遍歷包含字典裏面的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