所以,我想更新我的 items 表的某些字段:
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();
}