Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating multiple rows Linq vs SQL

Tags:

.net

sql

linq

Some time ago I wrote a piece of code to update multiple rows in a database table. The code was like this

var db = new MyDataContext();
db.Execute("UPDATE Details SET IsActive = 0 WHERE MasterId = 1");

Then the other day when I got the latest version of the file I saw that somebody changed the code to something like this

var details = from d in db.details where d.MasterId == 1 select d;
foreach (var detail in details)
  detail.IsActive = false;
db.SubmitChanges();

So my question is: What is the better way to update multiple rows? Using Linq or SQL?

like image 664
Marioh Avatar asked Dec 08 '08 00:12

Marioh


People also ask

Is LINQ faster than SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level.

What are the advantages of LINQ over SQL?

Advantages of Using LINQ LINQ offers the following advantages: LINQ offers a common syntax for querying any type of data sources. Secondly, it binds the gap between relational and object-oriented approachs. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support.

What is the difference between LINQ and SQL?

LinQ is a language integrated query. SQL is a structured query language. Linq is a simplified way to interact with any data source like collections, xml, database etc. While SQL only attracts with database.

What is LINQ advantages and disadvantages?

There are following disadvantages of using LINQ: LINQ is not good to write complex queries like SQL. LINQ doesn't take the full advantage of SQL features like cached execution plan for stored procedure. Performance is degraded if you don't write the LINQ query correctly.


1 Answers

Check the approach used in this article:

  • Batch Updates and Deletes with LINQ to SQL
like image 57
Christian C. Salvadó Avatar answered Oct 05 '22 03:10

Christian C. Salvadó