Delete Record using Linq

S

shapper

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
 
M

Marc Gravell

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
 
S

shapper

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?
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top