Chapter 6. Getting started with LINQ to SQL
This chapter covers:
- Using LINQ to SQL to access data from SQL Server
- The advantages of LINQ to SQL for fetching relational data
- Updating data with LINQ to SQL
So far in this book, we’ve focused on working with data once it is already in memory. In order to query it, we need a group of items we can iterate over using the IEnumerable<T> interface. While the ability to work with data already in memory is a much-needed feature, it fails to address a large part of the picture—moving data to and from a persistence medium, typically a relational database system like SQL Server.
By this point, we should be able to handle a fairly complex LINQ to Objects query. In this chapter, we’re going to take a look at converting a query and associated classes to use LINQ to SQL rather than custom ADO.NET. We’ll start with a query that will return a listing of books that cost less than $30 and group them by their subject. This will require us to combine information from two collections: Books and Subjects. Listing 6.1 represents a possible solution to the problem at hand.