C# 在 LINQ to Entities 中批量删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/869209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bulk-deleting in LINQ to Entities
提问by Benjamin Pollack
Is there any way to bulk-delete a bunch of objects matching a given query in LINQ or LINQ-to-Entities? The only references that I can find are outdated, and it seems silly to iterate over and manually delete all objects I wish to remove.
有没有办法批量删除一堆与 LINQ 或 LINQ-to-Entities 中的给定查询匹配的对象?我能找到的唯一引用已经过时,迭代并手动删除我想删除的所有对象似乎很愚蠢。
采纳答案by Shaul Behr
The question is an old one (from before EF5 existed). For anyone who's using EF5, EntityFramework.Extendeddoes this in a snap.
这个问题是一个老问题(在 EF5 存在之前)。对于使用 EF5 的任何人,EntityFramework.Extended可以快速完成此操作。
回答by Hemant
I know of DeleteAllOnSubmitmethod of any data context which will delete all the records in query. There must be some optimization underlying since a lot of objects are being deleted. I am not sure though.
我知道任何数据上下文的DeleteAllOnSubmit方法,它将删除查询中的所有记录。由于正在删除大量对象,因此必须进行一些优化。我不确定。
回答by Scott Anderson
I'm not sure how efficient it would be, but you could try something like this:
我不确定它的效率有多高,但你可以尝试这样的事情:
// deletes all "People" with the name "Joe"
var mypeople = from p in myDataContext.People
where p.Name == "Joe";
select p;
myDataContext.People.DeleteAllOnSubmit(mypeople);
myDataContext.SubmitChanges();
回答by HLGEM
YOu could write a stored proc that does the delete and call it from LINQ. A set-based delete is likely faster overall but if it affects too many records you could cause locking issues and you might need a hybrid of looping through sets of records (maybe 2000 at a time, size depends on your database design but 2000 is a starting place if you find the set-based delte takes so long it is affecting other use of the table) to do the delete.
您可以编写一个存储过程来执行删除操作并从 LINQ 调用它。基于集合的删除总体上可能会更快,但如果它影响太多记录,您可能会导致锁定问题,并且您可能需要混合循环遍历记录集(可能一次 2000,大小取决于您的数据库设计,但 2000 是一个如果您发现基于集合的 delte 花费的时间太长,以至于影响了表的其他使用)来进行删除。
回答by Alex James
A while back I wrote a 4 part blog series (Parts 1, 2, 3and 4) covering doing bulk updates (with one command) in the Entity Framework.
不久前,我写了一个由 4 部分组成的博客系列(第1 部分、第2 部分、第3 部分和第4 部分),涵盖了在实体框架中进行批量更新(使用一个命令)。
While the focus of that series was update, you could definitely use the principles involved to do delete.
虽然该系列的重点是更新,但您绝对可以使用所涉及的原则进行删除。
So you should be able to write something like this:
所以你应该能够写出这样的东西:
var query = from c in ctx.Customers
where c.SalesPerson.Email == "..."
select c;
query.Delete();
All you need to do is implement the Delete() extension method. See the post series for hints on how...
您需要做的就是实现 Delete() 扩展方法。有关如何...的提示,请参阅帖子系列。
Hope this helps
希望这可以帮助
回答by Phil Strong
The Answers I'm seeing here are Linq to Sql
我在这里看到的答案是 Linq to Sql
DeleteAllOnSubmit is part of System.Data.Linq and ITable which is Linq to Sql
DeleteAllOnSubmit 是 System.Data.Linq 和 ITable 的一部分,它是 Linq to Sql
This can't be done with Entity Framework.
实体框架无法做到这一点。
Having said all of that I don't have a solution yet but will post back when I do
说了这么多,我还没有解决方案,但会在我做的时候发回
回答by Vlad Bezden
using (var context = new DatabaseEntities())
{
// delete existing records
context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE CustomerID = {0}", customerId);
}
回答by Amin
Deleting data via the Entity Framework relies on using the DeleteObject method. You can call this method on the EntityCollection for the entity class you want to delete or on the derived ObjectContext. Here is a simple example:
通过实体框架删除数据依赖于使用 DeleteObject 方法。您可以在要删除的实体类的 EntityCollection 或派生的 ObjectContext 上调用此方法。这是一个简单的例子:
NorthwindEntities db = new NorthwindEntities();
IEnumerable<Order_Detail> ods = from o in db.Order_Details
where o.OrderID == 12345
select o;
foreach (Order_Detail od in ods)
db.Order_Details.DeleteObject(od);
db.SaveChanges();
回答by Uriil
For those who use EF6 and want to execute row SQL query for deletion:
对于那些使用 EF6 并希望执行行 SQL 查询进行删除的人:
using (var context = new DatabaseEntities())
{
// delete existing records
context.Database.ExecuteSqlCommand("DELETE FROM YOURTABLE WHERE CustomerID = @id", idParameter);
}
回答by G Jeny Ramirez
I'd do something like:
我会做这样的事情:
var recordsToDelete = (from c in db.Candidates_T where c.MyField == null select c).ToList<Candidates_T>();
if(recordsToDelete.Count > 0)
{
foreach(var record in recordsToDelete)
{
db.Candidate_T.DeleteObject(record);
db.SaveChanges();
}
}
I don't think there is a way to do it without a loop since Entity Framework works with Entities and most of the time, these means collection of objects.
我认为没有循环就没有办法做到这一点,因为实体框架与实体一起工作,而且大多数时候,这意味着对象的集合。