Santosh
Santosh

Reputation: 2495

how to use update statement for updating more than two rows at a time in linq

I have a table and I need to update 2 rows at a time.

My SQL query will look like this:

UPDATE tablename SET columnname=value WHERE tablename.id in (1,2)

I need the same in LINQ.

Thanks in advance.

Upvotes: 0

Views: 110

Answers (1)

Pleun
Pleun

Reputation: 8920

There is no such thing in LINQ to SQL. It will Always update one row at a time.

Of course, you can do a

foreach (var key in keys)
{
var r = db.Table.Single(i=>i.ID == key);
r.xyz = value;
}
db.submitchanges()

But essentially this will update each row with a single update statement. In a transaction, nonetheless so it will achieve the same as your update statement but with a significant performance penalty depending on the number of records

Upvotes: 1

Related Questions