RasmusKL RasmusKL - 12 days ago 6
SQL Question

Multiple deletions using LINQ (more specifically Linq2Nhibernate, but...)

Is there any smart way to do this?

If using

Linq2Nhibernate
, you really seem to have to rely on
HQL
or the likes to do multiple deletes from a database (without loading up and deleting one by one)?

It doesn't seem like
Linq2Sql
have it either? I just want something that can do stuff like:

DELETE FROM Accounts WHERE amount < 1000


Any ideas?

Answer

The short answer is: you can't.

You can do something like:

var q = from account in dataContext.Accounts
    where account.amount < 1000
    select account;

dataContext.DeleteAllOnSubmit(q);

But, because the framework needs to track concurrency issues it will always execute separate deletes (so if you have 500 rows that would be deleted, it will send 500 delete statements)

For a longer version and a solution see: http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx

Comments