Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to delete multiple records in a LINQ query?

People also ask

How do I delete a record in LINQ query?

You can delete rows in a database by removing the corresponding LINQ to SQL objects from their table-related collection. LINQ to SQL translates your changes to the appropriate SQL DELETE commands. LINQ to SQL does not support or recognize cascade-delete operations.

How we can truncate a table using LINQ?

context. ExecuteCommand("TRUNCATE TABLE Entity"); The way you are deleting is taking long because Linq to SQL generates a DELETE statement for each entity, there are other type-safe approaches to do batch deletes/updates, check the following articles: Batch Updates and Deletes with LINQ to SQL.


To delete records with Linq2Sql

CustomerDataContext ctx = new CustomerDataContext("connection string");
var customers = ctx.Customers.Where(c => c.Name == "david");

ctx.Customers.DeleteAllOnSubmit(customers);
ctx.SubmitChanges();

Here is How I solved the problem :

        try
        {
            List<MaterialPartSerialNumber> list = db.MaterialPartSerialNumbers.Where(s => s.PartId == PartId && s.InventoryLocationId == NewInventoryLocationId && s.LocationId == LocationId).ToList();
            db.MaterialPartSerialNumbers.RemoveRange(list);
            db.SaveChanges();
        }
        catch(Exception ex)
        {
            string error = ex.Message;
        }

First, you can find a list of the items you want to delete.

Then, you can use the function RemoveRange(**list_of_item_to_delete**) so that it removes each instance in the list present in the database.

According to the MSDN, the method removes a range of elements from the List.

For more information, check it here https://msdn.microsoft.com/en-us/library/y33yd2b5(v=vs.110).aspx


The good old SPROCs.....

You can drag the SPROC to your DBML file and it will generate a rich method in your databasecontext class.

  • How to: Call a Stored Procedure by Using LINQ (Visual Basic)
  • LINQ and Stored Procedures

Using entity framework 6

// Database context
EntitiesContext db = new EntitiesContext(connString);
// Select all the records to be deleted
IEnumerable<entity> list = db.entity.where(x=>x.id == id).toList();
// Use Remove Range function to delete all records at once
db.entity.RemoveRange(list);
// Save changes
db.SaveChanges();

Removing many records based on single where clause

context.EntityModel
            .RemoveAll(r => r.property == "propertyEntered");

But you could also Remove records from the database that don't exist in List<ListOfBadRecords>

context.EntityModel
            .Where(w => w.propertyID == ID).ToList()
            .RemoveAll(r => !ListOfBadRecords.Any(a => a.anyID == r.propertyID ));

Edit:

Unsure wich is quicker but you could do the 2nd query with this also

.RemoveAll(r => !ListOfBadRecords.Select(s=>s.propertyID ).Contains(w.propertyID ))

Edit2: don't forget context.SaveChanges(); as i did in the first draft