3

内部に辞書を含む配列リストをループしたい。

foreach(Dictionary<string, string> tempDic in rootNode) 
{ 
    Response.Write(tempDic.key + "," tempDic.value + "<br>"); 
} 

辞書のキーと値にアクセスするには?

4

2 に答える 2

4

tempDicまた、辞書内でループする必要があり、そのためにusing を繰り返すことができますForeach

foreach(Dictionary<string, string> tempDic in rootNode) 
{
    foreach(KeyValuePair<string, string> _x in tempDic)
    {
        Response.Write(_x.key + "," + _x.value + "<br>");
    }
}
于 2012-10-17T04:00:23.330 に答える
0

IEnumerable<KeyValuePair<string, string>>最初に LINQ を使用して、(実際には) すべてKeyValuePairの s のリストを取得できます。

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 ループを 1 回実行するだけで十分です。もう 1 つは LINQ によって行われます。

于 2012-10-17T04:23:28.240 に答える