2016-10-26 41 views
0

キーの最初のグループを列名にし、2番目のキーグループを行名にする必要があります。内側の辞書の値 これをC#のデータテーブルに変換するにはどうすればよいですか? ありがとうございます。 EDIT:行の名前は一意であり、辞書のキーとして存在します。辞書は行列のようなものです。辞書<文字列、辞書<文字列、文字列>>をデータテーブルに変換

答えて

0

ここに私の亀裂があります。

static DataTable DictionaryToDataTable(Dictionary<string,Dictionary<string,string>> dictionary) 
    { 
     int index; 
     string key = "YourPrimaryKeyColumnNameHere"; 
     DataTable dt = new DataTable(); 

     // Setting primary key allows you to perform Find() and IndexOf() later on 
     dt.Columns.Add(key); 
     dt.PrimaryKey = new DataColumn[] { dt.Columns[key] }; 

     foreach (var header in dictionary) 
     { 
      dt.Columns.Add(header.Key); 
      foreach (var row in header.Value) 
      { 
       index = dt.Rows.IndexOf(dt.Rows.Find(row.Key)); 

       // Index will be -1 if the row isn't found, ie on the first iteration 
       if (index < 0) 
       { 
        // When creating a new row, start it with the unique primary key field 
        dt.Rows.Add(new[] { row.Key }); 
        // We use dt.Rows.Count - 1 to get the last (newly added) row 
        dt.Rows[dt.Rows.Count - 1][header.Key] = row.Value; 
       } 
       else 
       { 
        dt.Rows[index][header.Key] = row.Value; 
       } 
      } 
     } 
     return dt; 
    } 
+0

チャームのように働いた!ありがとう! – anya

関連する問題