FREE BOOK

Chapter 11: An introduction to LINQ

Posted by Murach Free Book | LINQ August 13, 2009
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 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

Clause Description
from Identifies the source of data for the query.
where Provides a condition that specifies which elements are retrieved from the data source.
orderby Indicates how the elements that are returned by the query are sorted.
select Specifies the content of the returned elements.
let Performs a calculation and assigns an alias to the result that can then be used within the query.
join Combines data from two data sources.
group Groups the returned elements and, optionally, lets you perform additional operations
on each group.

Advantages of using LINQ

  • Makes it easier for you to query a data source by integrating the query language with C#.
  • Makes it easier to develop applications that query a data source by providing IntelliSense, compile-time syntax checking, and debugging support.
  • Makes it easier for you to query different types of data sources because you use the same basic syntax for each type.
  • Makes it easier for you to use objects to work with relational data sources by providing designer tools that create object-relational mappings.

Description

  • Language-Integrated Query (LINQ) provides a set of query operators that are implemented using extension methods. These methods are static members of the Enumerable and Queryable classes.
  • You can work with LINQ by calling the extension methods directly or by using C# clauses that are converted to calls to the methods at compile time.
  • A query that calls LINQ methods directly is called a method-based query. A query that uses C# clauses is called a query expression. You use a method-based query or query expression to identify the data you want to retrieve from a data source.
  • To use LINQ with a data source, the data source must implement the IEnumerable<T> interface or another interface that implements IEnumerable<T> such as IQueryable<T>. A data source that implements one of these interfaces is called an enumerable type.

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.

C# 2008 features that support LINQ

Feature Description
Query expressions Expressions with a syntax similar to SQL that can be used to retrieve and update data. Converted into method calls at compile time.
Implicitly typed variables Variables whose types are inferred from the data that's assigned to them. Used frequently in query expressions and with query variables.
Anonymous types An unnamed type that's created temporarily when a query returns selected fields from the data source.
Object initializers Used with query expressions that return anonymous types to assign values to the properties of the anonymous type.
Extension methods Provide for adding methods to a data type from outside the definition of the data type.
Lambda expressions Provide for coding functions inline. Used when extension methods are called directly.

LINQ providers included with C# 2008

Provider Description
LINQ to Objects Lets you query in-memory data structures such as generic lists and arrays.
LINQ to DataSet Lets you query the data in a typed or untyped dataset. See chapter 12 for more information.
LINQ to SQL Lets you query and update the data in a SQL Server database. See chapters 13 and 14 for more information.
LINQ to XML Lets you query and modify in-memory XML or the XML stored in a file. See chapter 16 for more information.
LINQ to Entities  Lets you query and update the data in any relational database. See chapter 18 for more information.

Description

  • C# 2008 provides several new features that are used to implement and work with LINQ. Most of these features can also be used outside of LINQ.
  • The LINQ providers perform three main functions: 1) They translate your queries into commands that the data source can execute; 2) They convert the data that's returned from the data source to the objects defined by the query; and 3) They convert objects to data when you update a data source.
  • LINQ to DataSet, LINQ to SQL, and LINQ to Entities are collectively known as LINQ to ADO.NET because they work with ADO.NET objects.
  • You can use the Object Relational Designer provided by Visual Studio to generate an object model for use with LINQ to SQL. See chapter 13 for information on how to use this designer.
  • You can use the Entity Data Model Designer provided by Visual Studio to generate an Entity Data Model for use with LINQ to Entities. See chapter 17 for information on how to use this designer.

Figure 11-2 C# features and providers that support LINQ

Total Pages : 10 12345

comments