DataTable users_table = db.GetRows("SELECT * FROM users");
int rowCount = users_table.Rows.Count;
int columnCount = users_table.Columns.Count;
Dictionary<int, Dictionary<string, string>> baskets = new Dictionary<int, Dictionary<string, string>>();
for (int i = 0; i < rowCount; i++)
{
baskets.Add(i, new Dictionary<string, string>());
for (int j = 0; j < columnCount; j++)
{
var colName = (users_table.Columns[i].ColumnName).ToString();
var colValue = users_table.Rows[i][j].ToString();
baskets[i].Add(colName, colValue);
Response.Write(baskets[i]["user_id"].ToString());
}
}
這是我得到的錯誤。如何在DataTable的aspx中創建多維字典?
ArrgumentException由用戶代碼在籃子[i] .Add(colName,colValue)處未處理;
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Additional information: An item with the same key has already been added.
我做錯了什麼?我調試它,「我」,「colName」和「colValue」的值是正確的。不知道爲什麼它給了我錯誤。 我是新手在.net編程
'字典'在.NET不允許冪等'Add'和'Set'操作(不像C++的'map',或PHP和Python中的關聯數組),所以你必須以不同的方式處理這兩種情況。 –
Dai