In this chapter, you'll learn the basic concepts and skills for using a new feature of C# 2008 called LINQ. To illustrate these concepts and skills, I'll use an implementation of LINQ called LINQ to Objects. You use LINQ to Objects to work with in-memory data structures such as generic lists and arrays. In section 3 of this book, for example, you saw that you frequently return the data from a database in a generic list. When you do that, you can use LINQ to Objects to query the data in that list. Although this implementation of LINQ isn't technically part of ADO.NET, it will help you understand the basic skills for using LINQ. And that will prepare you for learning how to use LINQ with datasets, relational databases, and XML.Basic concepts for working with LINQ
As its name implies, LINQ, or Language-Integrated Query, lets you query a data source using the C# language. Before you learn how to code LINQ queries, you need to learn some concepts related to LINQ, such as how LINQ is implemented and what the three stages of a query operation are. You'll also want to know about the new features of C# 2008 that support LINQ and the LINQ providers that are included with C# 2008. And you'll want to know about the advantages you'll get from using LINQ so you can decide for yourself if it's a feature you want to use.
How LINQ is implemented
LINQ is implemented as a set of methods that are defined by the Enumerable and Queryable classes. Because these methods can only be used in a query operation, they're referred to as query operators. Although you can call the query operators directly by coding a method-based query, you're more likely to use the clauses C# provides that give you access to the operators. When you use C# clauses to code a LINQ query, the result is called a query expression.
Figure 11-1 presents the C# clauses you're most likely to use in a query expression. If you've ever coded a query using SQL, you shouldn't have any trouble understanding what most of these clauses do. For example, you use the from clause to identify the data source for the query. You use the where clause to filter the data that's returned by the query. And you use the select clause to identify the fields you want to be returned by the query. You'll learn how to code queries that use all of these clauses later in this chapter.
Advantages of using LINQ
Figure 11-1 also lists several advantages of LINQ. Probably the biggest advantage is that it lets you query different types of data sources using the same language. In this chapter, for example, you'll see how to use LINQ to query a generic list of objects. Then, in the chapters that follow, you'll see how to use LINQ to query datasets, SQL Server databases, and XML.
The key to making this work is that the query language is integrated into C#. Because of that, you don't have to learn a different query language for each type of data source you want to query. In addition, as you enter your queries, you can take advantage of the IntelliSense features that are provided for the C# language. The compiler can catch errors in the query, such as a field that doesn't exist in the data source, so that you don't get errors at runtime. And when a runtime error does occur, you can use the Visual Studio debugging features to determine its cause.
Some of the C# clauses for working with LINQ
Description
Figure 11-1 An introduction to LINQ
Finally, if you're working with a relational data source such as a SQL Server database, you can use designer tools provided by Visual Studio to develop an object-relational mapping. Then, you can use LINQ to query the objects defined by this mapping, and the query will be converted to the form required by the data source. This can make it significantly easier to work with relational data sources.
C# 2008 features that support LINQ
C# 2008 introduced a variety of new features to support LINQ. These features are listed in the first table in figure 11-2. Most of these features can be used outside of LINQ. For the most part, though, you'll use these features when you code LINQ queries. You'll learn more about these features later in this chapter.
LINQ providers included with C# 2008
Figure 11-2 also presents the LINQ providers that are included with C# 2008. As I've already mentioned, you'll learn how to use the LINQ to Objects provider in this chapter to query generic lists. Then, in chapter 12, you'll learn how to use the LINQ to DataSet provider to query the data in a dataset. In chapter 13, you'll learn how to use the Object Relational Designer to create an object model for use with the LINQ to SQL provider, and you'll learn how to use that model to query a SQL Server database. Then, in chapter 14, you'll learn how to update the data in a SQL Server database using LINQ to SQL with an object model. In chapter 16, you'll learn how to use the LINQ to XML provider to load XML from a file, query and modify the XML in your application, and save the updated XML to a file. You'll also learn how to create XML documents and elements from scratch or from other documents and elements.
Another provider you can use with C# is LINQ to Entities. This provider works with an Entity Data Model that maps the data in a relational database to the objects used by your application. In chapter 17, you'll learn how to use the Entity Data Model Designer to create an Entity Data Model. Then, in chapter 18, you'll learn how to use LINQ to Entities to work with this model.
Figure 11-2 C# features and providers that support LINQ