2

我从 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 中删除行?

4

2 回答 2

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);
于 2013-05-04T18:49:20.590 回答
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);
}
于 2013-05-04T18:48:26.823 回答