
[英]What is the most effective way to delete entites that satisfy a condition from a database?
[英]Is there a way to Remove entites from database by Id in a single line without iterating each?
我有一个字符串List<string>
我从下面的方法var sentNotifications = Smtp.ProcessItems(queue, SmtpConfiguration);
我现在可以这样做:
foreach (var notification in sentNotifications)
{
//Retrieve entity by id
//Remove entity
}
但我不喜欢那样。 我能以某种方式检索组中的所有实体吗? 并同时删除它们?
如果您可以使用EF 7.0
,那么这里是批量删除:
context.Notifications
.Where(r => ids.Any(id => id == r.Id))
.ExecuteDelete();
我得到了答案:
_dbContext.Notifications.RemoveRange(_dbContext
.Notifications
.Where(r => ids.Any(id => id == r.Id)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.