Lance's Whiteboard

Random scribbling about C#, ASP.NET, Sql Reporting, etc.

News

BlogMailr Enabled




Sponsored Ad
Sponsored Ad

Blogs I Read

Developer Sites

Googling, etc...

MSDN Resources

Tutorials & Reference

FW: Batch Updates and Deletes with LINQ to SQL

I'm currently on a project creating a proprietary data-migration tool using C# & Linq.  I'm still new to Linq, but quickly discovered the challenges of doing mass-updates and deletes with Linq.

Specifically, by default Linq to Sql generates a sql statement for each row you are updating.  There is no built-in way to do large batch-updates or deletes without dropping to custom SQL.  After a quick search, I found this great article and sample code by Terry Aney on Batch Updates and Deletes with LINQ to SQL.

It offers solutions to many of the basic problems with some elegant extension methods so you can do things like:

   1:  be_Posts.UpdateBatch( first10, new { Author = "Chris Cavanagh" } );
   2:   
   3:  and 
   4:   
   5:  var posts = from p in be_Posts select p;   
   6:   
   7:  be_Posts.UpdateBatch( posts, p => new be_Posts { DateModified = p.DateCreated, 
   8:                                                   Author = "Chris Cavanagh" } );

Cool stuff!

Posted: Jun 23 2008, 06:07 PM by CodeSniper | with no comments
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)