Custom Software Blog Custom Software Consulting

Why use Linq instead of SQL?

by Tim Bussmann 4/3/2009 7:49:00 AM

To give you an idea of what Linq looks like, and how it compares to SQL code, below are some very simple queries in both SQL and Linq. This assumes a Customer table with columns ID and Name: 

SQL Linq
SELECT UPPER (c.Name)
FROM Customer AS c
WHERE c.Name LIKE '%a%'
ORDER BY LEN(c.Name)
from c in Customers
where c.Name.Contains ("a")
orderby c.Name.Length
select c.Name.ToUpper ()

Strongly Typed Queries
Linq queries are all strongly typed since classes are autogenerated to match your database schema. As a result, you also get intellisense all the way through composing a query. For example, in the above query, after typing "c." you would get an intellisense popup showing the properties of the Customer class, just like regular coding in Visual Studio. This also means that if you change the name of a table or column in the database, you no longer have to hunt through all your stored procedures to change references to the old name, instead you can just follow the trail of build errors in your code. You can even be tricky by doing name refactor from the old name to the new name before updating the schema.

More...

Currently rated 3.5 by 4 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

Powered by BlogEngine.NET 1.4.5.0