How to delete multiple records at one time using LINQ
-
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
-
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
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.