What is LINQ?
LINQ is short for Language Integrated Query. It is a Microsoft technology that allows programmers to query just about anything using SQL like syntax.
LINQ can be extended to support just about anything. For example, you could write something to allow you to query Flickr using LINQ. Microsoft has done an excellent job of making virtually all objects in C# 3.0 (or VB 9) queryable using LINQ. This means you can query lists, collection, objects, etc.
Why use LINQ?
Performance
Performance is extremely good with LINQ. The reason is that queries are optimized to only bring back the data you need. This is key for roundtrips to databases. In-memory queries are equally as optimized.
Strongly-Typed data
Strongly-typed data makes programming much easier and less error-prone. This also means that Intellisense works for all your objects. This gives the compiler a chance to help find issues with code changes.
No SQL Injection
LINQ provides protection from SQL Injection just a stored procedures in databases do. This is one of the major reasons I used stored procedures, and now I don’t have to.
Leverage Skills
Imagine being able to query anything using the same syntax and the same skill. This means you learn it once and use it everywhere. This is extremely useful and efficient. It also means that programmers can actually get away with not being well versed in SQL. Though, I think SQL and database knowledge is still very important. :) In terms of having to learn new API’s for services such as Flickr, Facebook, Amazon, etc, the learning curve is greatly reduced when using LINQ.
Future Standard
The general hope is that in 5 to 10 years all programming languages will have support to do queries as concept that is built into the language just like a for loop. Java appears to be moving in this direction with JPA (Java Persistance API) also. So, you might consider getting used to the idea and sharpening your skills early.
Less Code
When dealing with a database, you can have your entities that map to tables in your database automatically created for you. The mapping is editing using a visual designer in Visual Studio 2005 or later. The entity code is hidden from you, but you use partial classes to change the classes to suit your needs. This means much less code to maintain in your Data Access Layer. I still recommend a Data Access Layer so that you are not duplicating LINQ code and you are not tightly coupled to LINQ, but that is technically up to you.
Stored Procedure Support
Stored Procedures can still be used. This means if you prefer to use stored procedures you can. This is important in environments where databases are locked down and only allow access via stored procedures. The advantage is that you still don’t have to create or do much to maintain your entities.
List of LINQ Providers - What does LINQ work with?
The list is growing every day, but here is a good snapshot of what LINQ currently works with. Well, at least what I thought was pretty mainstream and useful. :) Each extension of LINQ has the naming conventions like LINQ to <thing> or LINQ over <thing> or <thing>LINQ. Most except the first few are created by third parties.
If you want to try to write your own, you might try this toolkit, though I have not tried it. Keep in mind, many times you don’t need a full blown provider. LINQ to Objects makes it possible to query most anything that is some sort of collection, list, etc.
- LINQ to Objects – works with IEnumerable, List, Array, Dictionary, etc.
- LINQ to SQL – Visual Studio supports this very well, and is what most people think of when they here LINQ. This allows you to use a Visual Designer to create and modify your entities.
- LINQ to Entities – Entities is short for the ADO.NET Entity Framework (EF). EF is essentially an Object/Relationship Mapper that Visual Studio 2008 SP1 handles quite well.
- LINQ to XML – Makes working with XML files much easier.
- LINQ to DataSet – Provides full query capabilities to your DataSets.
- DbLinq - LINQ to Oracle, MySQL, PostgreSQL, SQLite
- LINQ to CSV
- LINQ to MAPI
- LINQ to LLBLGen
- LINQ to NHibernate
- LINQ to Google
- LINQ to System Search – Windows Search
- LINQ to Amazon
- LINQ to LDAP
- LINQ to Flickr
- LINQ to SharePoint
- LINQ to Active Directory
- LINQ to Excel
- LINQ to JSON
- LINQ to JavaScript
- LINQ to WMI
- LINQ for PHP
- LINQ to FQL (Facebook)
If you want to see an example of how to actually use LINQ, I recommend my entry titled: LINQ – A brief overview