views:

22

answers:

1

I have a lot of rows to delete in a table, and using ADO.NET I might send a SqlCommand with this text:

 WHILE(1=1)  
BEGIN  
DELETE TOP(5000) FROM myTable WHERE myval=123  
 IF @@ROWCOUNT < 5000 BREAK  
END  

I would wrap that in a transaction or 2 and could then delete in chunks so the DB could come up for air.

I would like to do the same thing but in C# using LINQ-to-SQL, is that possible?

Thanks.

+1  A: 

Unfortunately, there is no mass update or mass delete operations in Linq-to-SQL (and with developement suspended on L2S, it's unlikely there ever will be)

The best you could do is

db.myTable.DeleteOnSubmit(db.MyTable.Where(m=>m.myval ==123).Take(5000));

But I'm pretty sure that will generate a SELECT followed by 5000 DELETE statements.

James Curran
That's not very nice, but I guess it is what it is. It smells like I'd need to get the total row count and do my own chunking in a C# loop and then do the delete in LinqtoSql.Thanks.
ScSub