Delete Record using Linq

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    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
  • Marc Gravell

    #2
    Re: Delete Record using Linq

    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

    Comment

    • shapper

      #3
      Re: Delete Record using Linq

      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?

      Comment

      • Marc Gravell

        #4
        Re: Delete Record using Linq

        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


        Comment

        Working...