-1

重複の可能性:
C#でネストされた辞書からデータをフェッチする方法

ネストされたDictionaryINC #からデータをフェッチする必要があります。私の辞書はこんな感じです:

static Dictionary<string, Dictionary<ulong, string>> allOffset = 
  new Dictionary<string, Dictionary<ulong, string>>();

次のように表される、完全な辞書のすべてのキー/値をフェッチする必要があります。

string->>ulong, string

前もって感謝します。

4

2 に答える 2

3

LINQ を使用してそれを行うことができます。

var flatKeysAndValues =
    from outer in allOffset    // Iterates over the outer dictionary
    from inner in outer.Value  // Iterates over each inner dictionary
    select new
               {
                   NewKey = outer.Key + "->>" + inner.Key,
                   NewValue = inner.Value
               };

使用例:

foreach (var flatKeysAndValue in flatKeysAndValues)
{
    Console.WriteLine("NewKey: {0} | NewValue: {1}", 
                             flatKeysAndValue.NewKey, flatKeysAndValue.NewValue);
}
于 2009-11-26T09:11:19.617 に答える
1

データをコンソールに書き込みたいのか、それとも新しいオブジェクト構造に変換したいのかわかりません。

ただし、印刷したいだけの場合は、これを試してください。

foreach( var pair in allOffset )
{
  foreach( var innerPair in pair.Value )
  {
    Console.WriteLine("{0}->>{1},{2}", pair.Key, innerPair.Key, innerPair.Value);
  }
}
于 2009-11-26T09:11:16.297 に答える