Best way to delete multiple records in a LINQ query?

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

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

10条回答
  •  伪装坚强ぢ
    2021-01-07 18:22

    Using entity framework 6

    // Database context
    EntitiesContext db = new EntitiesContext(connString);
    // Select all the records to be deleted
    IEnumerable list = db.entity.where(x=>x.id == id).toList();
    // Use Remove Range function to delete all records at once
    db.entity.RemoveRange(list);
    // Save changes
    db.SaveChanges();
    

提交回复
热议问题