13.2. Introducing LINQ to SQL

LINQ to SQL is actually LINQ to Microsoft SQL Server. With the official RTM of .NET 3.5 only SQL Server 2000 and 2005 are supported, although it's expected that third parties will release implementations that target other databases.

With LINQ to SQL, you take a bunch of database objects like tables and turn them into .NET objects that you can access in your code. You can then use these objects in queries or use them directly in data binding scenarios.

Working with LINQ to SQL is pretty easy and quite flexible. Using a diagram designer, you drag and drop objects from your database into your LINQ to SQL model. These objects can be tables, views, stored procedures, and even user-defined functions, although this chapter only looks at using tables in your diagrams. The database tables you drop on the diagram become available as objects. For example, if you drop the Review table on the diagram, you end up with a strongly typed Review class. You can create instances of this class using LINQ queries and other means as you'll see later in this chapter.

When you drop more than one related database table on your diagram, the LINQ to SQL designer detects the relationships between the tables that you created in your database. The designer then replicates these relationships in your object model. For example, if you had a Review instance created in code using some LINQ query (as you'll see later), you could access its Genre property, which in turn gives you access ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.