所以,我想更新我的項目表中的某些字段:LINQ2SQL表:表<fieldname>作爲參數
internal void FieldsUpdate(string id, System.Collections.Hashtable fieldsWithChanges, List<string> keysToUpdate)
{
using (DBDataContext db = new DBDataContext())
{
item myItem = db.items.Single(t=>t.id==id);
keysToUpdate.ForEach(key => {
myItem[key] = GetValue(fieldsWithChanges, key).ToString();
//Or something like that, I'm inventing that index
});
db.SubmitChanges();
}
}
myItem[key]
不存在,那麼應該怎麼辦呢?
這是可怕的選擇,我知道的:
internal void FieldsUpdate(string id, System.Collections.Hashtable fieldsWithChanges, List<string> keysToUpdate)
{
using (DBDataContext db = new DBDataContext())
{
item myItem = db.items.Single(t=>t.id==id);
keysToUpdate.ForEach(key => {
if (key=="thisKey")
myItem.thisKey = GetValue(fieldsWithChanges, key).ToString();
// really awful. So What this condition for every colname? thats unsustainable.
});
}
db.SubmitChanges();
}
'keys'應該是'keysToUpdate'嗎? – Khan
你是對的,先生。 (但這不是問題,但我會改變它以避免混淆)謝謝! – apacay