Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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;<br />db.Role.DeleteAllOnSubmit(result);<br />db.SubmitChanges();<br />

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

Posted

1 solution

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&lt;int&gt;();<br />list.Add(5);<br />list.Add(9);<br />list.Add(58);<br />list.Add(7);<br /><br />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%.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900