tags:

views:

44

answers:

4

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;
A: 

Are you looking for Select?

Stephen Cleary
no actually....
Sadegh
In that case, see Frank's answer. LINQ by default can only be used to *select* data, not update it.
Stephen Cleary
+1  A: 

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.

ck
how? please describe in more depth thanks ;)
Sadegh
+1  A: 

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!

Frank Tzanabetis
thanks, so i can't do that and also thanks for provided links. I got the answer
Sadegh
+1  A: 

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.

Alex Humphrey
ok exactly that i'm want to have ;)
Sadegh