Bulk-deleting in LINQ to Entities
The question is an old one (from before EF5 existed). For anyone who's using EF5, EntityFramework.Extended does this in a snap.
How to Delete multiple records in Linq to Entity?
You can do the following, which is technically still a loop.
obj.tblA.Where(x => x.fid == i).ToList().ForEach(obj.tblA.DeleteObject);
obj.SaveChanges();
The alternative is calling the SQL directly.
Deleting multiple records with Entity Framework using a single LINQ query
Install Entity Framework Extended Library
(PM> Install-Package EntityFramework.Extended)Import EntityFramework.Extensions in your code
Delete records specified by inner query
context.Orders.Where(o=>o.User_ID == 1).Delete();
Deletes all records inside Orders with userID = 1
How do I delete multiple rows in Entity Framework (without foreach)
If you don't want to execute SQL directly calling DeleteObject in a loop is the best you can do today.
However you can execute SQL and still make it completely general purpose via an extension method, using the approach I describe here.
Although that answer was for 3.5. For 4.0 I would probably use the new ExecuteStoreCommand API under the hood, instead of dropping down to the StoreConnection.
bulk delete in entity framework
There is an interesting NuGet package that lets you do batch deletes and updates:
Linq to Entities Remove without fetching
You can have a look at the EntityFramework.Extended Library which offers you to write the below query:
//delete all Settings where UserId matches
db.Settings.Where(s=> s.UserId == Uid).Delete();
Docs:
A current limitations of the Entity Framework is that in order to
update or delete an entity you have to first retrieve it into memory.
Now in most scenarios this is just fine. There are however some
scenarios where performance would suffer. Also, for single deletes,
the object must be retrieved before it can be deleted requiring two
calls to the database. Batch update and delete eliminates the need to
retrieve and load an entity before modifying it.
Related Topics
When Is It Appropriate to Use C# Partial Classes
When Should Taskcompletionsource<T> Be Used
Does .Net Have a Way to Check If List a Contains All Items in List B
Merging Multiple PDFs Using Itextsharp in C#.Net
Differencebetween "X Is Null" and "X == Null"
Get Controller and Action Name from Within Controller
Why Can't a Duplicate Variable Name Be Declared in a Nested Local Scope
How to Bind a List<String> to a Datagridview Control
Pinvoke for C Function That Returns Char *
Entity Framework Refresh Context
Uwp Binding in Style Setter Not Working
String Concatenation Using '+' Operator