Bulk-deleting in LINQ to Entities

c# database entity-framework linq linq-to-entities

Question

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.

1
79
5/15/2009 7:58:47 PM

Accepted Answer

The question is an old one (from before EF5 existed). For anyone who's using EF5, EntityFramework.Extended does this in a snap.

30
11/22/2012 3:17:34 PM

Popular Answer

A while back I wrote a 4 part blog series (Parts 1, 2, 3 and 4) covering doing bulk updates (with one command) in the Entity Framework.

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...

Hope this helps



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow