Bim*_*Das 4 c# asp.net-mvc entity-framework asp.net-mvc-3 asp.net-mvc-4
我需要从ID 列表中删除多个ID.
public IHttpActionResult Delete(List<string> IDs)
{
DealBazarEntities.Restaurants.RemoveRange(IDs);
DealBazarEntities.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
但是RemoveRange不允许多个ID,它只是期望List<entities>.
是的,我知道,如果我将实体列表发送到服务器而不是发送ID列表,那么我可以很容易地实现这一点.但我不喜欢那样.
同样,我不想使用foreach循环遍历每个ID.
根据问题中Stephen Muecke评论部分的回答答案,解决方案是:
DealBazarEntities.Restaurants.RemoveRange
(DealBazarEntities.Restaurants.Where(r => IDs.Contains(r.ID)));
Run Code Online (Sandbox Code Playgroud)