« 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)

The only way buyers can have utmost guarantee that the jewelry they have just bought are tiffany & co silver rings true is if they are from the authentic maker. Otherwise, chances of being deceived when buying replica Tiffany and Co jewelry are high because fake replicas are very real. Nevertheless, customers can still mark these if they are looking for the genuine replicas in a market that is flooding with the company’s tiffany necklace uk. Accessing the craftsmanship of each piece they could be interested in is very critical because any Tiffany’s jewelry depict unrivaled manufacturing techniques.

August 26, 2011 | Unregistered Commentertiffany jewellery

Certainly, owning the actual Cheap authentic NFL jerseys, we perform ball games could be thought because formly much more. So my personal dear everyone, if a person still have not got your own authentic NFL jerseys, bet you are able to consider my personal choice, follow to purchase the inexpensive Wholesale Cheap authentic NFL jerseys. CAPITAL T o end up being honest, i hope everyone can train lots of sports and obtain the Cheap authentic NFL jerseys body simply because health may be the living bottom. To achieve this, we may live much more happily as well as longer. Bless everybody, my buddies.

November 25, 2011 | Unregistered CommenterCheap authentic NFL jerseys

gned uggs on sale

It should likewise be mentioned ugg boots sale that both the uggs for cheap fake and the important products generally are designed uggs on sale in uggs on sale China www.fashionbootskindom.net
Related:
1 2 3 4

November 29, 2011 | Unregistered Commentersunnyfel

In this modern and fashionable society, people are pursuing for ugg australia boots cool, unique, stylish and innovative. Whether it is uggs australia uk or fashion accessories all means a lot for modern society of today. Same is the case with trendy looking australia ugg boots. When these are cheap Ugg Boots, the excitement just gets doubled. Most chic looking UGG Classic Cardy Boots are in fashion now. These are one of the favorite fashion accessories for men and women long time ago. If you have not yet tried UGG Ultra Tall Boots, it's time to own one and feel the difference it can make to your personality. These are just brilliant and fabulous uk ugg boots sale. They are most iconic and can provide you with a new feeling and enhance confidence. The quality of discount uggs is just superior to what you have dreamt of. Today owning a new and trendy looking ugg boots sale are not only meant for the wealthy people. These are now made luxurious and affordable Cheap ugg Boots to reach out to every budget and range. You can just enjoy them by ordering cheap Ugg Boots online where you get the complete satisfaction and genuine quality at best possible rates. The finish, quality and designs you get from ugg boot sale are really astonishing and you will love them all. Different styles and designs of uk ugg boots sale are now available to make your wrist beautiful. You can choose from the wide variety of ugg boots on sale by comparing lots of perfect and stunning pieces. These UGG Ultra Short Boots would be nice investments for you in long term. These UGG Classic Mini Boots are brilliant and prove to be wonderful for you while it makes you stand out of the crowd. The dashing and extraordinary cheap boots you get will attract every eye in a crowd and makes you feel more confident. If you are not having ugg boots cheap yet, check them out and own them now as were not so affordable ever before. You will not miss them out. Be it Christmas or Valentine's Day or any other special day that you would like to celebrate, a Ugg Boots sale gift would be just perfect for any occasion. Many people think of the perfect discount uggs to purchase for their loved ones. Even if you shop for your special occasion at the last moment, you are sure to find something nice at ugg nightfall boots. UGG Sundance II Boots gifts are not restricted to just your lover, you could also pick perfect for almost everyone, including your family and friends.

November 30, 2011 | Unregistered CommenterUgg Boots sale

e is burberry sale

The only problem here is burberry sale that you should burberry sale online clearly understand the exact size burberry outlet of the foot and most importantly,Since www.burberrysalee.com you are buying boots online,Be sure the trainers are certified burberry sale by highly,and burberry handbags other extra,not all bootcamps will burberry scarves be the very same,light silver,Even online business www.burberrysaleo.com prosperious
Related:
1 2 3 4 5

December 29, 2011 | Unregistered Commenterhandshery

With thunder two years and $6.5 million contract renewal

8. With thunder two years and $6.5 million contract renewal of KuiAn-cook. Cook in 2007 the first round of the 76 draft night 21 people team selected,cheap nike air max,
cheap nike air max 24/7,
nike shoes outlet,
cheap nike air max 90,
cheap nike air max 2012,
cheap nike air max shoes the four seasons has played in the heat and thunder. In the 222 games, cook averaging can get 7.5 points and 2.3 rebounds. On behalf of his team last season thunder 43 games,Wholesale nba Jerseys,cheap nhl Jerseys,Wholesale NFL Jerseys , NBA Jerseys,cheap mlb jerseys, NFL Throwback Jerseys,cheap nfl Jerseys,cheap NBA Jerseys cook all present 13.9 minutes with the can get 5.6 points and 1.7 rebounds, his 3-point shot 42.2%. And the three points is the lack of the thunder,cheap air max,
cheap air max shoes,
cheap jordan shoes,
cheap air max 90,
nike air max 2012,
air max shoes this is also out of contract he thunder decided to reason.cheap nfl jerseys,
cheap nhl jerseys,
football jerseys,
nfl football jerseys,
cheap nfl football jerseys,
cheap mlb jerseys,
cheap nba jerseys,
nfl jerseys cheap,
wholesale nfl jerseys
9.76 people team this summer signing first cheap nba shoes rookie nicola-wu's cut and second round picks pull walter Iraq Allen. Wu's cut from nba basketball jerseys,
cheap nba basketball shoes,
nba jerseys,
cheap nba shoes,
cheap NBA Throwback Jerseys,cheap nba jerseys the university of southern California, he in the first round of the 16 th overall 76 team selected, and walter Iraq is in total 50 overall pick.Green Bay Packers NFL Jerseys,
San Francisco 49ers NFL Jerseys,
Baltimore Ravens NFL Jerseys,
New Orleans Saints NFL Jerseys,
Pittsburgh Steelers NFL Jerseys,
New York Giants NFL Jerseys,
New England Patriots NFL Jerseys,
New York Jets NFL Jerseys,Cheap nfl jerseys,
nfl jerseys,
cheap mlb jerseys,nfl jerseys from china,cheap nhl jerseys,wholesale nfl jerseys,nfl jerseys cheap In addition, 76 people and free player Antonio Anderson, dwyane-Jones,wholesale authentic jerseys,
cheap authentic jerseys,cheap nhl jerseys,
wholesale authentic nfl jerseys,cheap mlb jerseys,
cheap authentic nfl jerseys.
Green Bay Packers NFL Jerseys
San Francisco 49ers NFL Jerseys
Baltimore Ravens NFL Jerseys
New Orleans Saints NFL Jerseys
Pittsburgh Steelers NFL Jerseys
New York Giants NFL Jerseys
New England Patriots NFL Jerseys mike-of Atlantis, and Xavier-sheila, so that 76 people of the book's list to reach 15 people.Herve Leger,
Herve Leger on sale,
authentic Herve Leger,
wholesale Herve Leger,
cheap Herve Leger,
Herve Leger Dress

January 10, 2012 | Unregistered Commenternflstorechina

With thunder two years and $6.5 million contract renewal

8. With thunder two years and $6.5 million contract renewal of KuiAn-cook. Cook in 2007 the first round of the 76 draft night 21 people team selected,cheap nike air max,
cheap nike air max 24/7,
nike shoes outlet,
cheap nike air max 90,
cheap nike air max 2012,
cheap nike air max shoes the four seasons has played in the heat and thunder. In the 222 games, cook averaging can get 7.5 points and 2.3 rebounds. On behalf of his team last season thunder 43 games,Wholesale nba Jerseys,cheap nhl Jerseys,Wholesale NFL Jerseys , NBA Jerseys,cheap mlb jerseys, NFL Throwback Jerseys,cheap nfl Jerseys,cheap NBA Jerseys cook all present 13.9 minutes with the can get 5.6 points and 1.7 rebounds, his 3-point shot 42.2%. And the three points is the lack of the thunder,cheap air max,
cheap air max shoes,
cheap jordan shoes,
cheap air max 90,
nike air max 2012,
air max shoes this is also out of contract he thunder decided to reason.cheap nfl jerseys,
cheap nhl jerseys,
football jerseys,
nfl football jerseys,
cheap nfl football jerseys,
cheap mlb jerseys,
cheap nba jerseys,
nfl jerseys cheap,
wholesale nfl jerseys
9.76 people team this summer signing first cheap nba shoes rookie nicola-wu's cut and second round picks pull walter Iraq Allen. Wu's cut from nba basketball jerseys,
cheap nba basketball shoes,
nba jerseys,
cheap nba shoes,
cheap NBA Throwback Jerseys,cheap nba jerseys the university of southern California, he in the first round of the 16 th overall 76 team selected, and walter Iraq is in total 50 overall pick.Green Bay Packers NFL Jerseys,
San Francisco 49ers NFL Jerseys,
Baltimore Ravens NFL Jerseys,
New Orleans Saints NFL Jerseys,
Pittsburgh Steelers NFL Jerseys,
New York Giants NFL Jerseys,
New England Patriots NFL Jerseys,
New York Jets NFL Jerseys,Cheap nfl jerseys,
nfl jerseys,
cheap mlb jerseys,nfl jerseys from china,cheap nhl jerseys,wholesale nfl jerseys,nfl jerseys cheap In addition, 76 people and free player Antonio Anderson, dwyane-Jones,wholesale authentic jerseys,
cheap authentic jerseys,cheap nhl jerseys,
wholesale authentic nfl jerseys,cheap mlb jerseys,
cheap authentic nfl jerseys.
Green Bay Packers NFL Jerseys
San Francisco 49ers NFL Jerseys
Baltimore Ravens NFL Jerseys
New Orleans Saints NFL Jerseys
Pittsburgh Steelers NFL Jerseys
New York Giants NFL Jerseys
New England Patriots NFL Jerseys mike-of Atlantis, and Xavier-sheila, so that 76 people of the book's list to reach 15 people.Herve Leger,
Herve Leger on sale,
authentic Herve Leger,
wholesale Herve Leger,
cheap Herve Leger,
Herve Leger Dress

January 10, 2012 | Unregistered Commenternflstorechina

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>