473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete Record using Linq

Hello,

I have 3 tables, Tags, ArticlesTags and FilesTags, with the following
columns:

Tags TagID, Text

ArticlesTags TagID, ArticleID

FilesTags TagID, FileID

I need to delete all records in Tags which are not in ArticlesTags and
FilesTags.

If a Tag is present in one of the 2 tables, ArticlesTags or FilesTags,
then it will not be deleted.

How would I do this?

Thanks,

Miguel
Mar 7 '08 #1
3 2172
Just a thought - in this scenario, you aren't really interested in the
fields of the things you are deleting, so I wouldn't bother feftching
them from the database (which will be necessary [or at the least, all
the PKs of such] if you want LINQ to delete them).

If this was me, I'd write a stored procedure that used a few NOT
EXISTS checks, and drag the SP into LINQ (juts to simplify the calling
mechanism).

LINQ is a great tool, but you can mix-and-match generated code and SPs
so that each does things it is good at.

Marc
Mar 7 '08 #2
On Mar 7, 4:56 am, Marc Gravell <marc.grav...@g mail.comwrote:
Just a thought - in this scenario, you aren't really interested in the
fields of the things you are deleting, so I wouldn't bother feftching
them from the database (which will be necessary [or at the least, all
the PKs of such] if you want LINQ to delete them).

If this was me, I'd write a stored procedure that used a few NOT
EXISTS checks, and drag the SP into LINQ (juts to simplify the calling
mechanism).

LINQ is a great tool, but you can mix-and-match generated code and SPs
so that each does things it is good at.

Marc
Hi,

I know ... but in this moment I would like to use only Linq for
this ...
.... later I will probably use a SP.

I came up with:

Dim database As New CodeDataContext
Dim tags = From t In database.Tags _
Where Not (t.FilesTags.An y Or t.ArticlesTags. Any) _
Select t
database.Tags.D eleteAllOnSubmi t(tags)
database.Submit Changes()

It is working. Just one question:
What do you mean with getting only the ID?
How do I do that in my code?

Thanks,
Miguel

P.S: Sorry for my VB.NET code but I am more used to is and it is
faster for me.
When will converters work also with Linq code?
Mar 7 '08 #3
What do you mean with getting only the ID?
How do I do that in my code?
I don't know if it is possible; but it would be theoretical bare-minimum to
perform a delete...

But I stress; if it was me, I wouldn't be getting the data out of the
database...

Marc
Mar 7 '08 #4

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

Similar topics

2
5476
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
24217
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
2
1466
by: shapper | last post by:
Hello, I have a ListView defined as follows: Private Sub lv_Init(ByVal sender As Object, ByVal e As EventArgs) Handles lv.Init lv.DataKeyNames = New String() {"TagID"} lv.ID = "lv" lvTags.ItemTemplate = New MyItemTemplate() lvTags.LayoutTemplate = New MyLayoutTemplate()
3
1931
by: plonk | last post by:
Hi I'm trying to learn sql by getting a database up and running on VB.net 2008 and SQL server express. I have the database connected correctly, (i think) but when i try to add a record using this code.. Dim db As New racedataDataContext Dim newmeeting As MeetingInfo = New MeetingInfo newmeeting.Date = Today db.MeetingInfos.add(newmeeting)
1
4968
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
1669
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
8122
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!
0
2352
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
3698
by: Big Daddy | last post by:
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 (LogicDataContext ctx = new LogicDataContext(m_strConnect)) { JobQueue jq = ctx.JobQueues.Single(queue =queue.JobQueueNo ==
0
7987
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
8472
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
8464
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
8130
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
6805
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
3954
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...
0
4015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2464
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1574
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.