hi, i have a query like below and i want to update all elements of sequence. but without using foreach/for
var res = _context.tbl1.Where(/* Conditions here */);
res.//Using a method to performing changes;
hi, i have a query like below and i want to update all elements of sequence. but without using foreach/for
var res = _context.tbl1.Where(/* Conditions here */);
res.//Using a method to performing changes;
You could write an extension method for IEnumerable<T>
that did iterate and perform the action you required, potentially using a delegate or Func to make it more generic.
If you're asking whether you can do set based updates in linq-to-sql as you can in sql, i'm afraid you can't. You either have to individually update each l2s object and call SubmitChanges()
when you're done, or call ExecuteCommand()
with your sql query.
Here's a thread you might be interested in - http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/0f014318-5259-43c7-8518-06948cec465e
Edit: Actually, this may be of interest to you - http://stackoverflow.com/questions/348417/updating-multiple-rows-linq-vs-sql
If you have a look there's a link to here - http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx which looks kinda cool. Might have to check it out myself!
Write an extension method for it
static class IEnumerableExtensions
{
public static void ForEach(this IEnumerable<T> seq, Action<T> action)
{
foreach (var item in seq)
{
action(item);
}
}
}
But note that under the hood you're still looping through - just like the Where extension method and all the others. The extension methods hide the looping.