LINQ to SQL can be an excellent keystone to mastering LINQ at a deep conceptual and technical level. Think about it. LINQ (pronounced "link"), or Language INtegrated Query, is an integrated query language that lets you manipulate everything from objects to databases to XML, and LINQ can be extended into other areas like SharePoint and Active Directory. Consider LINQ to SQL. LINQ is its own query language. When used with SQL LINQ converts code-queries into SQL on the fly. Understanding how to write LINQ queries is a good beginning. Mastery comes with grokking how LINQ might convert its queries into SQL queries. With this mastery comes the ability to use LINQ and extend LINQ into new spaces like Active Directory.
The fundamental reason you would choose LINQ over SQL for data, XPath for XML, C# or VB for code, and dsquery for Active Directory (and whatever else you can think of) is that LINQ is a "learn once, apply many" (times and ways) kind of technology.
When using LINQ to SQL you can begin by adding a LINQ to SQL Classes element from the Add New Item dialog (a .dbml file). Drag a couple of tables from the Server Explorer onto the object relational designer--like Northwind Customers and Orders--and the object relational designer (see Figure 1) will infer the association and use the CodeDOM to code generate smart entity classes. (You can generate an object relational map to explore the generated code.) At this point you need to create a context, which is analogous to a SqlConnection, and query away. Use LINQ to read data and context methods to write data.
Figure 1: The Visual Studio object relational designer will infer relationships and code generate entity classes; all you have to do is drag and drop tables from the Server Explorer.
Listing 1 demonstrates how easy it is to query the Northwind (or any SQL database with LINQ to SQL). Just a few lines of code and you are off to the races. What is worth noting here is that I assigned Console.Out to the DataContext.Out property letting LINQ to SQL show us a bit about what its doing (see Figure 2). (If you want exhaustive detail about how all of this works check out my book LINQ Unleashed for C# from Sams. And, consider writing a review if you like it.)
Listing 1: Reading data with LINQ to SQL takes just a couple of lines of code; all of the ADO.NET plumbing is managed for you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqToSQLBlog
{
class Program
{
static void Main(string[] args)
{
CustomersAndOrdersDataContext context =
new CustomersAndOrdersDataContext();
context.Log = Console.Out;
var customers = from customer in context.Customers
where customer.CompanyName.StartsWith("A")
select customer;
Array.ForEach(customers.ToArray(),
c=>Console.WriteLine(c.CompanyName));
Console.ReadLine();
}
}
}
Figure 2: Notice that LINQ is sending some information about its internal workings--the LINQ to SQL conversion--to the console.
How does LINQ do it. The answer is that the .NET framework contains an interface for LINQ called IQueryProvider. IQueryProvider is a small interface. It defines two methods: CreateQuery and Execute. CreateQuery accepts input and you make substitutions using the expression tree capability of LINQ and Execute runs the query represented by the expression tree. If you are interested in LINQ to SQL relative to IQueryProvider start exploring Table<TEntity>'s implementation of IQueryProvider using Reflector's disassembler feature.