如何运行一个大规模更新/删除查询中的LINQ?LINQ

2023-09-03 03:32:35 作者:得不到的永远在骚动L

我有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: 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/ [可能是正确的] 的