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
or
update Child set Val = Val+1 where ParentID = @p
I can do this in Linq the brute force way inside the Parent class:
Children.ToList().ForEach(c => c.DeleteOnSubmit()); // DeleteOnSubmit is my own method
and
Children.ToList().ForEach(c => c.Val++);
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?
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.wordpress.com/2008/04/14/batch-updates-and-deletes-with-linq-to-sql/
[Probably the correct one]