Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to delete multiple records at one time using LINQ

How to delete multiple records at one time using LINQ

Scheduled Pinned Locked Moved LINQ
databasecsharplinqtutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pranavcool
    wrote on last edited by
    #1

    I know I can use the SQL delete recorde at one time

    Delete * from my table where ID in (5,9,58,7)

    If I use LINQ, I have to write

    var result = from r in db.Role where (r.ID =5) or (r.ID =9) or (r.ID =58) or (r.ID =7) select r;
    db.Role.DeleteAllOnSubmit(result);
    db.SubmitChanges();

    Is there a simple way (another way) to do that in LINQ?

    Pranav Dave

    P 1 Reply Last reply
    0
    • P pranavcool

      I know I can use the SQL delete recorde at one time

      Delete * from my table where ID in (5,9,58,7)

      If I use LINQ, I have to write

      var result = from r in db.Role where (r.ID =5) or (r.ID =9) or (r.ID =58) or (r.ID =7) select r;
      db.Role.DeleteAllOnSubmit(result);
      db.SubmitChanges();

      Is there a simple way (another way) to do that in LINQ?

      Pranav Dave

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      What you would do is create a list of IDs that you want to delete and then use the Contains statement. Here's an example:

      List<int> list = new List<int>();
      list.Add(5);
      list.Add(9);
      list.Add(58);
      list.Add(7);

      var result = from r in db.Role where list.Contains(r.ID) select r;

      I've just knocked this up in the Code Project textbox, so I apologise if the formatting isn't 100%.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups