如何运行一个大规模更新/删除查询中的LINQ? [英] How to run a mass update/delete query in Linq?

查看:148
本文介绍了如何运行一个大规模更新/删除查询中的LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 LINQ2SQL类:孩子。我想要做的事情,如消除所有的孩子为父母,或更新所有子记录。在SQL中我会写:

I have 2 Linq2Sql classes: Parent and Child. I want to do things like removing all children for a parent, or updating all child records. In SQL I would have written:

delete Child where ParentID = @p

update Child set Val = Val+1 where ParentID = @p

我可以在LINQ的做到这一点的蛮力方式父里面类:

Children.ToList().ForEach(c => c.DeleteOnSubmit()); // DeleteOnSubmit is my own method

Children.ToList().ForEach(c => c.Val++);

但鉴于LINQ的对foreach循环固有的性能损失,这似乎是一个非常低效的方式来做事。有没有达到预期的结束,将火过的一些方法只有一个查询?

But given Linq's inherent performance penalty on ForEach loops, this seems like a very inefficient way to do things. Is there some way of achieving the desired end that will fire off exactly one query?

推荐答案

看看这个链接:它使用防爆pressionTree: <一href="http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx" rel="nofollow">http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx [断开链接]

Look at this link : It's using ExpressionTree : http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx [Broken Link]

http://terryaney.word$p$pss.com/2008/04/14/batch-updates-and-deletes-with-linq-to-sql/ [可能是正确的]

这篇关于如何运行一个大规模更新/删除查询中的LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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