mha.dk

Random thoughts of wisdom - the personal blog of Michael Holm Andersen

Updating multiple rows using LINQ

LINQ to SQL does not give you a way to specify a set-based update against the database (not yet, anyway!)

The only way to update multiple entities/rows is to retrieve, modify and update them via individual SQL UPDATE statements when you call SubmitChanges, which does not perform very well.

So in order to update multiple rows you have to rely on Stored Procedures or plain 'old' SQL - the easiest way to execute SQL using the DataContext class is this way:

using (NibefestivalDataContext db = new NibefestivalDataContext())
{
       db.ExecuteCommand("UPDATE Employee Set FirstName = '{0}' WHERE FirstName Like '%{1}%'", "ABC", "B");
}

I totally agree: This is NOT pretty, however it works! :-)