我从DB中选择一个id,然后我想删除带有此id的行:
var ado = new mydbEntities();
var selItem = listView3.SelectedItems[0];
if (selItem != null)
{
var selId = (from t in ado.task
where t.t_name == selItem.Text
select new { t.id});
ado.task.DeleteOnSubmit(selId); //this command return error
}
我尝试使用DeleteOnSubmit
删除,但是我收到错误。
如何从DB中删除行?
答案 0 :(得分:1)
编辑:由于您似乎正在使用ObjectSet
,因此您需要使用DeleteObject();
var sel = (from t in ado.task
where t.t_name == selItem.Text
select t).FirstOrDefault();
if(sel != null)
ado.task.DeleteObject(sel);
DeleteOnSubmit()将实体作为参数,而不是匿名类型。这应该会更好;
var sel = (from t in ado.task
where t.t_name == selItem.Text
select t).FirstOrDefault();
if(sel != null)
ado.task.DeleteOnSubmit(sel);
如果要删除多个实体,请使用DeleteAllOnSubmit(),其中包含可枚举的实体;
var sel = from t in ado.task
where t.t_name == selItem.Text
select t;
ado.task.DeleteAllOnSubmit(sel);
答案 1 :(得分:0)
var ado = new mydbEntities();
var selItem = listView3.SelectedItems[0];
if (selItem != null)
{
var tasksToDelete = from t in ado.task
where t.t_name == selItem.Text
select t;
ado.task.DeleteOnSubmit(tasksToDelete);
}