Lance's Whiteboard

Random scribbling about C#, Javascript, Web Development, Architecture, and anything else that pops into my mind.

News


Creative Commons License
Lance's Whiteboard Blog by Lance Hunt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Based on a work at weblogs.asp.net




Sponsored Ad
Sponsored Ad

Blogs I Read

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
Filed under: , ,

Comments

No Comments