删除LINQ查询中的多个记录的最佳方法?

时间:2009-05-15 18:41:08

标签: linq linq-to-sql

使用LINQ一次删除多个记录的最佳方法是什么?

10 个答案:

答案 0 :(得分:26)

使用Linq2Sql删除记录

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

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

答案 1 :(得分:2)

好老的SPROCs .....

您可以将SPROC拖到DBML文件中,它将在您的databasecontext类中生成一个rich方法。

答案 2 :(得分:2)

以下内容更多适用于LINQ to Entities,但它可能有所帮助:

Bulk-deleting in LINQ to Entities

答案 3 :(得分:2)

使用实体框架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();

答案 4 :(得分:2)

根据单个where子句删除许多记录

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

但您也可以删除数据库中不存在于List<ListOfBadRecords>

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

编辑:

不确定哪个更快但你也可以用这个进行第二次查询

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

编辑2:不要像我在初稿中那样忘记context.SaveChanges();

答案 5 :(得分:1)

也许这个答案有点迟,但今天我自己遇到了这个需求我和我想出了这个解决方案

CustomerDataContext ctx = new CustomerDataContext("connection string");

ctx.Customers.DeleteAllOnSubmit(ctx.Customers.Where(c => c.Name == "david"));

ctx.SubmitChanges();

答案 6 :(得分:1)

以下是我如何解决问题:

        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;
        }

首先,您可以找到要删除的项目列表。

然后,您可以使用函数RemoveRange(**list_of_item_to_delete**),以便删除数据库中存在的列表中的每个实例。

根据MSDN,该方法从List中删除了一系列元素。

有关详细信息,请在此处查看https://msdn.microsoft.com/en-us/library/y33yd2b5(v=vs.110).aspx

答案 7 :(得分:0)

我同意Khurram,使用LINQ的简单存储过程来执行此操作会更有效(假设您在SQL中有足够的权限来执行此操作)。我将以一个例子来补充这一点。

存储过程:

CREATE PROCEDURE [dbo].[RemovePermissionsFromRole] 
(
    @ROLE_ID int
)
AS
BEGIN
    SET NOCOUNT ON;

    DELETE FROM [RolePermissions] WHERE [RoleID] = @ROLE_ID;
END

将存储过程从数据库资源管理器拖到DBML文件的方法窗格中。保存文件。在代码中:

if (Request.QueryString["RoleID"] != null) {
    int roleID = Convert.ToInt32(Request.QueryString["RoleID"]);

    SETSDataContext context = new SETSDataContext();
    context.RemovePermissionsFromRole(roleID);
}

答案 8 :(得分:0)

这是我使用的,首先创建一个表的IENumerable对象,其中要删除的记录是,然后只使用RemoveRange,最后只保存对数据库的更改。我们假设您要从Products表中的一个特定供应商ID中删除所有产品,这就是您可以这样做的方式。

IEnumerable ProductsToRemove = db.Products.Where(x =&gt; x.SupplierId == Supplierid); db.Products.RemoveRange(ProductsToRemove); db.SaveChanges();

答案 9 :(得分:0)

仅使用Entity Framework,我发现这是最严格的代码。

db.PreperProperties.RemoveRange(db.PreperProperties.Where(c => c.preperFk == prpr.id));
db.SaveChanges();