7

Possible Duplicate:
Bulk-deleting in LINQ to Entities

I need to remove some entities by condition. E.g., remove all order items having quantity less than 1:

var orderId = 10; // any order Id
context.OrderItems.RemoveWhere(item => item.OrderId == orderId && item.Quantity < 1.0);

I know, that I can select those items and then remove them one-by-one like this:

var itemsToRemove = context.OrderItems.Where(item => item.OrderId == orderId && item.Quantity < 1.0).ToArray();

foreach (var item in itemsToRemove)
  context.OrderItems.Remove(item);

But this is very unlikely, because extra work will take place. Am I missed something?

Community
  • 1
  • 1
Dennis
  • 37,026
  • 10
  • 82
  • 150

1 Answers1

3

You could use the EntityFramework.Extended plugin on GitHub which has support for Batch Update and Delete.

James
  • 80,725
  • 18
  • 167
  • 237
  • This solution has some limitations, but it is better, than nothing. Thanks, I'll try it. – Dennis Jul 25 '12 at 11:50
  • @Dennis it's open source so you can always tweak it to suit your requirements. – James Jul 25 '12 at 12:00
  • @james the limitations are more theoretical/practical in nature. There is no generic way for an ORM to properly support Batch Update and Delete. It's still largely an open research problem in academic computer science slanted towards practical software engineering - see Willard Cook's "batching" paper for some of the latest. – John Zabroski Jun 15 '15 at 13:44
  • @JohnZabroski I never said there was, my answer was specifically targeted towards the EF. Ultimately, an ORMs is just a layer that sits between your app & the database to simplify/speed up development, it's by no means a silver bullet. There are many situations where bypassing the ORM completely is necessary for performance/efficiency needs. – James Jun 15 '15 at 13:56