By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

Delete Record using Linq

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
On Mar 7, 4:56 am, Marc Gravell <marc.grav...@gmail.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.Any Or t.ArticlesTags.Any) _
Select t
database.Tags.DeleteAllOnSubmit(tags)
database.SubmitChanges()

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.