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?
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.
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.
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.
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.
Check the approach used in this article:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With