简体   繁体   English

使用实体框架从 ID 列表中删除多个项目

[英]Delete multiple Items from a List of Id's using Entity Framework

I need to delete multiple Ids from a List of Ids.我需要从 ID列表中删除多个 ID。

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

But RemoveRange does not allow multiple ids , it expect only List<entities> .但是RemoveRange不允许多个 ids ,它只需要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.是的,我知道,如果我将实体列表发送到服务器而不是发送 ID 列表,那么我可以轻松完成此操作。 But I don't like that.但我不喜欢那样。

Again, I don't want to use foreach loop to loop through every Ids.同样,我不想使用foreach循环遍历每个 ID。

According to the answer of Stephen Muecke 's answer given into comment section in the question , the solution is :根据Stephen Muecke在问题的评论部分中给出的答案,解决方案是:

DealBazarEntities.Restaurants.RemoveRange
(DealBazarEntities.Restaurants.Where(r => IDs.Contains(r.ID)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM