如何在实体框架中使用下面的代码

时间:2015-04-28 09:14:18

标签: c# asp.net

delete asp_testtable
where  userid='XYZ' 
and    SL=12
and    userAmount=13

其中:

  • asp_testtable是表名
  • useridvarchar
  • 类型的列
  • SLbigint
  • 类型的列
  • userAmountdecimal
  • 类型的列

这是我遵循的三个步骤,但没有它们正在运作

第1步:

asp_testtable map = DBContext.asp_testtable.RemoveRange(DBContext.asp_testtable.Where(c => c.userid == tbuserid.Text.Trim() && c.SlNo == Convert.ToInt64(lbSlNo.Text.ToString()) && c.userAmount == Convert.ToInt16(ddluserAmount.SelectedValue.ToString()))).SingleOrDefault();

DBContext.asp_testtable.Remove(map);

第2步:

var y = (from x in asp_testtable where x.userid == tbuserid.Text.Trim() && x.SlNo == Convert.ToInt64(lbSlNo.Text) && x.userAmount == Convert.ToInt16(ddluserAmount.SelectedValue) select x.FeeMappingSlNo).First();

DBContext.asp_testtable.RemoveRange(y);

第3步:

feedetail = new asp_testtable()
{
    userid = tbuserid.Text.Trim(),
    SlNo = Convert.ToInt64(lbSlNo.Text),
    userAmount = Convert.ToInt16(ddluserAmount.SelectedValue)
};

DBContext.asp_testtable.Remove(feedetail);

2 个答案:

答案 0 :(得分:1)

context.asp_testtable.Select(item => item.userid == "XYZ" && item.SL = 12 && 
item.userAmount == 13).ToList().ForEach(context.asp_testtable.DeleteObject);
context.SaveChanges();

找到相关项目并使用DeleteObject

将其删除

答案 1 :(得分:0)

试试这个:

string DeleteStatement = "DELETE FROM asp_testtable " + 
    "WHERE userid = 'XYZ' AND SL=12 AND userAmount=13 ";

yourcontext.Database.SqlQuery(DeleteStatement);