Best way to delete multiple records in a LINQ query?

后端 未结 10 768
暗喜
暗喜 2021-01-07 17:22

What is the best way to remove multiple records in one go with LINQ?

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 18:04

    This is what I used, first create an IENumerable object of the table where are the records to be removed are, then just use RemoveRange, and finally just save changes to database. Let's say you want to remove all products from one specific supplier ID on the Products table, this is how you could do that.

    IEnumerable ProductsToRemove= db.Products.Where(x => x.SupplierId== Supplierid); db.Products.RemoveRange(ProductsToRemove); db.SaveChanges();

提交回复
热议问题