Posts why LINQ beats SQL
Post
Cancel

why LINQ beats SQL

reference : https://www.linqpad.net/WhyLINQBeatsSQL.aspx

SQL is a very old language—invented in 1974. Since then it’s been extended endlessly, but never redesigned. This has made the language messy. You might have become so accustomed to this that you can’t see anything wrong!

-Write a simple query that retrieves customers as follows:

1
2
3
4
SELECT UPPER(Name)
FROM Customer
WHERE Name LIKE 'A%'
ORDER BY Name

That doesn’t look too bad, right? But now suppose these results are feeding a web page, and we want to retrieve just rows 21-30. Suddenly, you need a subquery:

1
2
3
4
5
6
7
8
9
SELECT UPPER(Name) FROM
(
   SELECT *, RN = row_number()
   OVER (ORDER BY Name)
   FROM Customer
   WHERE Name LIKE 'A%'
) A
WHERE RN BETWEEN 21 AND 30
ORDER BY Name

Not only is this complicated and messy, but it violates the DRY principle (Don’t Repeat Yourself). Here’s same query in LINQ. The gain in simplicity is clear:

1
2
3
4
5
6
7
var query =
   from c in db.Customers
   where c.Name.StartsWith ("A")
   orderby c.Name
   select c.Name.ToUpper();

var thirdPage = query.Skip(20).Take(10);

origin - http://www.pipiscrew.com/?p=2170 net-why-linq-beats-sql

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags