Home > Back-end >  Is there a way to Remove entites from database by Id in a single line without iterating each?
Is there a way to Remove entites from database by Id in a single line without iterating each?

Time:01-30

I have a List of strings List<string> which I get from the following method var sentNotifications = Smtp.ProcessItems(queue, SmtpConfiguration);

I can do this now:

        foreach (var notification in sentNotifications)
        {
            //Retrieve entity by id
            //Remove entity
        }

But I would not like that. Can I somehow retrieve all entities in a group? And delete them all at the same time?

CodePudding user response:

If you can use EF 7.0 then here is the bulk delete:

context.Notifications
           .Where(r => ids.Any(id => id == r.Id))
           .ExecuteDelete();

CodePudding user response:

I got the answer:

  _dbContext.Notifications.RemoveRange(_dbContext
           .Notifications
           .Where(r => ids.Any(id => id == r.Id)));
  • Related