你好,我嘗試做類似PHP這是從數據庫中檢索數據和存儲在二維集合(字典)的東西 我不知道我寫的是否正確。C# - 從數據庫中檢索數據並存儲在2維字典中?
比方說,我的數據庫表和預期結構的結果是這樣的(見截圖)
public ActionResult ShowBook()
{
var books = from v in db.Books
select v;
Dictionary<string, Dictionary<string, string>> test = new Dictionary<string, Dictionary<string, string>>();
foreach (var item in books)
{
test[item.Book_Type_ID][item.Author_ID] = item.Book_Name;
}
return .....
}
但我有這個錯誤
System.Collections.Generic.KeyNotFoundException:
「給定的鍵不存在在字典裏。'
我該怎麼辦?
大先生回答 – Jonesopolis