« jQuery Tag Suggestion in ASP.NET MVC | Main | Award winning application »
Tuesday
Aug252009

Linq to SQL delete multiple/bulk records (batch delete)

Normally when building ASP.NET applications Linq to SQL makes the job a whole lot easier. It takes care of all the data access for creating, updating and deleting individual records. However every so often I come across the need to delete multiple records such as the case with a one to many relationship, where I need to delete all related records for a specific group. Unfortunately this is not as simple as you would think using Linq to SQL as Linq to SQL only deals with one record at a time. For a large high performance application where large numbers of records need to be deleted the best approach would be to use a stored procedure. In some cases the delete is contained to small datasets and would not be used frequently so keeping all of your code in Linq to SQL keeps it clean and consistent. In this case I have found the sample code below does the trick.

MyAppDataContext db = new MyAppDataContext();

var deleteRelatedRecords =
from relatedRecords in db.RelatedRecords
where relatedRecords.MyForeignKeyID == MyPrimaryKeyID
select relatedRecords;

foreach (var relatedRecords in deleteRelatedRecords)
{
db.RelatedRecords.DeleteOnSubmit(RelatedRecords);
}

db.SubmitChanges();

This code works well if all you need to do is clean up/delete a small group of records, it first gets the group of records, then loops through the dataset flagging each one to be deleted. Once completed the changes are submitted committing the deletes.

Additional information and samples in other languages can be found here http://msdn.microsoft.com/en-us/library/bb386925.aspx

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (46)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: fake diamond rings
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: curtis debord
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: curtis debord
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Post Brothers
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: post brothers
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: curtis debord
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Post Brothers
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Bolig
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Bolig
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Bolig
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: handverkere
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: curtis debord
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: .UxS_n4XcOjQ
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Leger
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: link k schwartz
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: link k schwartz
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: kate dircksen
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: link k schwartz
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: link k schwartz
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Douglas Pitassi
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: link schwartz
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: douglas pitassi
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Forsikring
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Post Brothers
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Post Brothers
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: Bradley Kurgis
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: sexercise routine
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: ray ban outlet
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: sroczka.xdt.pl
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: wholesale jerseys
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: sexercise
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: stripe skirt
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    Response: vps hosting
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)
  • Response
    kmsystems - Web Application Development - Journal - Linq to SQL delete multiple/bulk records (batch delete)

Reader Comments (28)

I believe you can accomplish the same thing without your 'foreach' loop and let LTS do this for you? Instead of :
foreach (var relatedRecord in deleteRelatedRecords)
{
db.RelatedRecords.DeleteOnSubmit(relatedRecord);
}

You can use:
db.RelatedRecords.DeleteAllOnSubmit(deleteRelatedRecords);

It is probably true that LTS just calls the same foreach that you've shown, but at least you don't need to write it yourself...

September 22, 2009 | Unregistered CommenterFunka!

Hi Funka thanks for your comment, reducing a few lines of code is great thanks again.

September 22, 2009 | Registered CommenterDavid Abrahams

Cool,

This is a great instruction on how to batch delete

Keep up the good work

January 6, 2010 | Unregistered Commenterweb development

While most deaths occurred in elderly men, this scare has prompted many men to seek natural alternatives and herbal supplements that provide results either comparable to Viagra
or identical to Viagra online
. As soon as Cialis Online
appeared on the market, the manufacture of different generic versions took off.

August 12, 2010 | Unregistered Commenterviagra

This is pure gold discount Pandora Jewellery beads and K Gold a confusion, one of the characteristics of pure gold is soft, so when the time inlaid stones off as easily strong enough. Therefore, it was added in gold silver, copper, zinc and other metals to increase strength and toughness of gold, the gold produced in this way, also discount Pandora Jewelry bracelets known as K gold. In almost all cheap Pandora Jewellery inlaid with different numbers of K K gold to inlay precious stones.

September 24, 2010 | Unregistered Commenterpan

This is a good post, I stumbled across your article while looking for song downloads. Thanks for sharing, I’ll be sure to recommend this site to others.

October 19, 2010 | Unregistered Commenterugg boots sale

Nice post. This post is different from what I read on most blog. And it have so many valuable things to learn. Thank you for your sharing!

October 19, 2010 | Unregistered Commenterugg boots sale

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets .ugg boots sale.Thanks for this post. This was a very interesting article. Thanks once more I will visit again

October 25, 2010 | Unregistered Commenterugg boots sale

I can not do no wrong my earlier he finished, splash, kneel toward asics sneakers,Asics Revolve, that I am negative teacher life, to punish my fault, I Gan Yuanshou punished Her Majesty the Queen

onitsuka tiger shoes furious and denounced the channel you how have you this apprentice Iowa Do you know how your master is dead Is to be your family in front of the mountains a beast to kill Sun’s you actually give them kneel down You stand up for me

Kneeling is still in place, said wife, the former things,asics gel running shoes, I do not know, but I know the master mind, if he is still the elderly, he probably will not want to see you now look like this more, the real harm the Master’s asics gel kahana 3 Carlos Moya also

Dead for so many years, why should your

You gave me to shut up onitsuka tiger shoes,Asics Alton, a slap in the face approached, the life ye angry Iowa Road is a bad old man’s death to be offset by the good you plead for a family of Mountain Sun, I’ll kill you, A China does not need you this useless apprentice he finished, beat Hong Xiang’s

November 22, 2010 | Unregistered Commenterasics shoes

Bright idea, hope there can be more useful articles about pandora jewellery uk.
It has been long before I can find some useful articles about pandora bracelets. Your views truly open my mind.
I totally agree with you on the point of pandora charms. This is a nice article for sure.

December 21, 2010 | Unregistered Commenterpandora jewellery

New Air Max 2011 is the most popular of running shoes. The Nike Air Max 2010 shoes design, by high performance style. Nike Air Max Zenyth is a kind of wear-resisting, deft, comfortable and fashion Mens Nike Air Max LTD shoes.
Nike Air Max Skyline is special for sports and of the design.This is a high quality of Nike Air Max BW shoes and wear comfortable Air Max 90 shoes, easy. If you are a sports, that we suggest you buy Cheap Air Max 2011|Women's Nike Air Max 4-27|Nike Air Max TN shoes, you will not regret it.
Every woman longs to have set their own victoria bikini,Put on fashionable and beautiful victorias Secret bikini| Sinful Bikini playing on the seashore is every woman's dream,But, there are other words of good, the most beautiful may not be the most suitable, the most suitable is the most beautiful。Wear on the, the world is you. This summer, you wear Ed Hardy Bikini ?
Want to buy a sexy Chanel Bikinis|Sinful Bikinis|ED Hardy Bikinis|Hollister Bikinis that can win you all the attention this summer? Get into this for the latest collection of sexy Louis Vuitton Bikinis|Gucci Bikini|juicy Bikinis swim wear swim wear and oakley sunglass sale.

July 27, 2011 | Unregistered Commenternike air max 2010

Cheap Philadelphia Eagles jerseys, Cheap Pittsburgh Steelers jerseys and Wholesale NFL Jerseys are accessible in altered stores. There certainly are a aggregate of on the net aliment throughout ne can swap for these wholesale adornment jerseys. When affairs an precise affordable Cheap San Diego Chargers jerseys, you ability turn out attributable Cheap NFL Jersey priceless, collector's jersey. timberland boots However, the actuality may be the reality that abounding people these times do not apperceive what they accept purchased in Cheap San Francisco 49ers jerseys Store. As there are positively abundant replica jerseys with positively best exceptional and beautiful attending that appear such as the complete ones, Cheap Seattle Seahawks jerseys is positively tougher to acquaint which could be authenticCheap Washington Redskins jerseys, but in Cheap Tampa Bay Buccaneers jerseys online. You could possibly get NFL jerseys broad of all types.

August 10, 2011 | Unregistered CommenterCheap NFL Jersey

Hope there will be more wonderful things in your blog. Replica oakleys discussion - it is very occupied. cheap oakley sunglassesi will continue to read. fake oakleys Can I have this copied? I will link back to your blog. Is that ok? I replica oakley sunglasses have bookmarked it for you so that others will be able to Fake Oakley Sunglasses feeling very worthwhile, very happy.

August 19, 2011 | Unregistered Commentercheap oakley sunglasses

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>