使用linq删除行
本文关键字:删除行 linq 使用 | 更新日期: 2023-09-27 18:03:57
如何使用linq和实体框架从数据库中删除记录。
public void DeleteCartItem(string customerId, string storeId, string productId)
{
//Authenticate service
Authenticate();
int _customerId = Convert.ToInt32(customerId);
int _storeId = Convert.ToInt32(storeId);
int _productId = Convert.ToInt32(productId);
var _cartid = (from c in context.carts
where c.CustomerId == _customerId && c.StoreId == _storeId && c.ProductId ==_productId
select c.CartId);
context.carts.Remove();
}
上面提到的代码不工作。我想根据在这个方法中传递的三个参数来删除
如果您正在使用EntityFramework 6那么您可以尝试如下:
public void DeleteCartItem(string customerId, string storeId, string productId)
{
//Authenticate service
Authenticate();
int _customerId = Convert.ToInt32(customerId);
int _storeId = Convert.ToInt32(storeId);
int _productId = Convert.ToInt32(productId);
var _cartid = (from c in context.carts
where c.CustomerId == _customerId && c.StoreId == _storeId && c.ProductId ==_productId
select c);
context.carts.RemoveRange(_cartid );
context.SaveChanges();
}
或者一个快速的:
context.carts.RemoveRange(context.carts.Where(c => c.CustomerId == _customerId && c.StoreId == _storeId && c.ProductId ==_productId));
context.SaveChanges();