12

I need to delete multiple Ids from a List of Ids.

public IHttpActionResult Delete(List<string> IDs)
{
    DealBazarEntities.Restaurants.RemoveRange(IDs);
    DealBazarEntities.SaveChanges();
}

But RemoveRange does not allow multiple ids , it expect only List<entities>.

Yes, I know that , if I send list of entities to server instead of sending List of ids , Then I can easily accomplish this. But I don't like that.

Again, I don't want to use foreach loop to loop through every Ids.

2
  • 7
    DealBazarEntities.Restaurants.RemoveRange(DealBazarEntities.Restaurants.Where(r => IDs.Contains(r.ID))); Commented Feb 14, 2016 at 7:32
  • 1
    Wow... It's working like a charm.... Thanks. You saved my lots of time again. :) Commented Feb 14, 2016 at 8:07

1 Answer 1

16

According to the answer of Stephen Muecke's answer given into comment section in the question , the solution is :

DealBazarEntities.Restaurants.RemoveRange
(DealBazarEntities.Restaurants.Where(r => IDs.Contains(r.ID)));
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, but note that when I profiled this, it executed an initial SELECT and then separate DELETE statements per matched ID, rather than one DELETE that used IN. So it's not quite as efficient as it could be.
@JohnnyHK I think you can execute raw sql command from EF in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.