How do I delete multiple rows in Entity Framework (without foreach)

Posted by Jon Galloway on Stack Overflow See other posts from Stack Overflow or by Jon Galloway
Published on 2010-03-25T22:24:26Z Indexed on 2010/03/25 22:33 UTC
Read the original article Hit count: 1252

Filed under:

I'm deleting several items from a table using Entity Framework. There isn't a foreign key / parent object so I can't handle this with OnDeleteCascade.

Right now I'm doing this:

var widgets = context.Widgets
    .Where(w => w.WidgetId == widgetId);

foreach (Widget widget in widgets)
{
    context.Widgets.DeleteObject(widget);
}
context.SaveChanges();

It works but the foreach bugs me. I'm using EF4 but I don't want to execute SQL. I just want to make sure I'm not missing anything - this is as good as it gets, right? I can abstract it with an extension method or helper, but somewhere we're still going to be doing a foreach, right?

© Stack Overflow or respective owner

Related posts about entity-framework