473,756 Members | 6,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to delete a record using LINQ?

I am trying to do all my DB access through LINQ. For example, I am
trying to delete a record from the JobQueue table. There’s a couple
ways I could do this:

1. Method 1 – The disadvantage is that it makes 2 calls to the DB: one
to get the record and then one to delete it:

using (LogicDataConte xt ctx = new LogicDataContex t(m_strConnect) )
{
JobQueue jq = ctx.JobQueues.S ingle(queue =queue.JobQueue No ==
queueNo);
ctx.JobQueues.D eleteOnSubmit(j q);
ctx.SubmitChang es();
}

2. Method 2 – The disadvantage is that I’m writing SQL directly, which
I don’t want to do. In fact, it’s the whole reason I’m using LINQ.

using (LogicDataConte xt ctx = new LogicDataContex t(m_strConnect) )
{
string str = "Delete JobQueue WHERE JobQueueNo = " + queueNo;
ctx.ExecuteComm and(str);
}

3. Method 3 – This doesn't work because it throws an exception: "Row
not found or changed." It doesn't make two trips to the DB like
method 1, but instead tries to attach a JobQueue to the data context.
But will only work if all the column values are exactly the same as
the values in the DB:

using (LogicDataConte xt ctx = new LogicDataContex t(m_strConnect) )
{
JobQueue jq = new JobQueue() { JobQueueNo = requestQueueNo };
ctx.JobQueues.A ttach(jq, false);
ctx.JobQueues.D eleteOnSubmit(j q);
ctx.SubmitChang es();
}

Is there a better way? I think LINQ should have a built in function
to delete a record using the primary key.

I guess if I was going to use the second method, I could write a
helper function that would use reflection to create the DELETE
statement using the table name and the fact that the primary key
column property has an attribute on it like this:

[Column(Storage= "_JobQueueN o", AutoSync=AutoSy nc.OnInsert,
DbType="Int NOT NULL IDENTITY", IsPrimaryKey=tr ue,
IsDbGenerated=t rue)]

Thanks in advance,
John
Nov 19 '08 #1
0 3719

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2569
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before submit, which determines exactly where to send the form contents. The table of "receipients" could contain in the region of 3,500 recipients but is more likely to contain up to 1,000. Table structure:
2
5480
by: shapper | last post by:
Hello, I have two tables: TagId, PostId TagId, PostId How can I delete, given a TagId, the record from Tags and all records associated with it in PostTags.
3
24225
by: shapper | last post by:
Hello, I have a table named Tags with the following columns: TagId and TagValue. How can I delete a record, using LINQ, given the TagId value. Thanks, Miguel
1
4976
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a record with the same primary key) and I run SubmitChange it generates an key duplicate exception. I am reviewing the SQL commands generated by linq, and I see that attempts to run first insert command, and then delete command.
2
1673
by: news.microsoft.com | last post by:
Hello people: First of all forgiveness for my English. I am working with Linq to SQL, Visual Studio 2008 and SQL Server 2000. When you delete a record and then go back to Insert (ie, insert a record with the same primary key) and I run SubmitChange it generates an key duplicate exception. I am reviewing the SQL commands generated by linq, and I see that attempts to run first insert command, and then delete command.
3
2179
by: shapper | last post by:
Hello, I have 3 tables, Tags, ArticlesTags and FilesTags, with the following columns: Tags TagID, Text ArticlesTags TagID, ArticleID FilesTags TagID, FileID
2
8129
by: Arjen | last post by:
Hi, How can I delete multiple records with Linq? On the internet I found the DeleteAll function, but this one does not exist in my library. Please help me out. Thanks!
4
1530
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the coordinates to a recording service, such as: /Record.ashx?X=123&Y=456
0
2362
by: mesut | last post by:
Hi, I'm using LINQ.. and I would like to understand if it's a bug or not in LINQ... I've a situation like: I'm reading all records via "Select" Statement and a specific select statment #1 record retrieves the record and propose for Delete or Update... when I make changes and click on Update I get "DuplicateKeyException Cannot add an entity with a key that is already in use. "
0
9212
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9779
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6473
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.