【发布时间】:2013-08-01 14:11:09
【问题描述】:
所以,我想更新我的 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();
}
【问题讨论】:
-
keys应该是keysToUpdate吗? -
你说得对,对不起。 (但这不是问题,但我会更改它以避免混淆)谢谢!
标签: c# linq tsql linq-to-sql