所以,我想更新我的项目表中的某些字段: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