Linq to Sql:如何快速清除表 [英] Linq to Sql: How to quickly clear a table

查看:262
本文介绍了Linq to Sql:如何快速清除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要删除表中的所有行,我目前正在执行以下操作:

To delete all the rows in a table, I am currently doing the following:

context.Entities.DeleteAllOnSubmit(context.Entities);
context.SubmitChanges();

但是,这似乎需要花很多时间.有没有更快的方法?

However, this seems to be taking ages. Is there a faster way?

推荐答案

您可以使用

context.ExecuteCommand("TRUNCATE TABLE Entity");

由于Linq to SQL会为每个实体生成DELETE语句,因此删除方法的时间很长,还有其他 type-safe 方法可进行批量删除/更新,请查看以下文章:

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
  • LINQ to SQL Extension: Batch Deletion with Lambda Expression

这篇关于Linq to Sql:如何快速清除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆