Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 21 INTRODUCTION TO LINQ

The Structure of Query Expressions

A query expression consists of a from clause followed by a query body, as illustrated in Figure 21-3. Some of the important things to know about query expressions are the following:

The clauses must appear in the order shown.

The two parts that are required are the from clause and the select...group clause.

The other clauses are optional.

In a LINQ query expression, the select clause is at the end of the expression. This is different than SQL, where the SELECT statement is at the beginning of a query. One of the reasons for using this position in C# is that it allows Visual Studio’s IntelliSense to give you more options while you’re entering code.

There can be any number of from...let...where clauses, as illustrated in the figure.

Figure 21-3. The structure of a query statement consists of a from clause followed by a query body.

546

CHAPTER 21 INTRODUCTION TO LINQ

The from Clause

The from clause specifies the data collection that is to be used as the data source. It also introduces the iteration variable. The important points about the from clause are the following:

The iteration variable sequentially represents each element in the data source.

The syntax of the from clause is shown following, where

Type is the type of the elements in the collection. This is optional, because the compiler can infer the type from the collection.

Item is the name of the iteration variable.

Items is the name of the collection to be queried. The collection must be enumerable, as described in Chapter 13.

Iteration variable declaration

from Type Item in Items

The following code shows a query expression used to query an array of four ints. Iteration variable item will represent each of the four elements in the array and will be either selected or rejected by the where and select clauses following it. This code leaves out the optional type (int) of the iteration variable.

int[] arr1 = {10, 11, 12, 13};

Iteration variable

 

 

var query = from item in

arr1

← Uses the iteration variable

where item <

13

select item;

 

← Uses the iteration variable

foreach( var item in query ) Console.Write("{0}, ", item );

This code produces the following output:

10, 11, 12,

547

CHAPTER 21 INTRODUCTION TO LINQ

Figure 21-4 shows the syntax of the from clause. The type specifier is optional, since it can be inferred by the compiler. There can be any number of optional join clauses.

Figure 21-4. The syntax of the from clause

Although there is a strong similarity between the LINQ from clause and the foreach statement, there are several major differences:

The foreach statement executes its body at the point in the code where it is encountered. The from clause, on the other hand, does not execute anything. It creates an enumerable object that’s stored in the query variable. The query itself might or might not be executed later in the code.

The foreach statement imperatively specifies that the items in the collection are to be considered in order, from the first to the last. The from clause declaratively states that each item in the collection must be considered but does not assume an order.

548

CHAPTER 21 INTRODUCTION TO LINQ

The join Clause

The join clause in LINQ is much like the JOIN clause in SQL. If you’re familiar with joins from SQL, then joins in LINQ will be nothing new for you conceptually, except for the fact that you can now perform them on collections of objects as well as database tables. If you’re new to joins or need a refresher, then the next section should help clear things up for you.

The first important things to know about a join are the following:

A join operation takes two collections and creates a new temporary collection of objects, where each object contains all the fields from an object from both initial collections.

Use a join to combine data from two or more collections.

The syntax for a join is shown here. It specifies that the second collection is to be joined with the collection in the previous clause.

Keyword

Keyword

Keyword

Keyword

join Identifier in Collection2 on Field1 equals Field2

 

Specify additional collection

 

The fields to compare

and ID to reference it

 

for equality

Figure 21-5 illustrates the syntax for the join clause.

Figure 21-5. Syntax for the join clause

The following annotated statement shows an example of the join clause:

First collection and ID

 

 

 

 

 

 

 

 

 

Item from first collection

Item from second

var query = from s in students

 

 

 

 

 

join c in studentsInCourses on s.StID equals c.StID

 

 

 

 

 

 

 

 

Second collection and ID

Fields to compare

549

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]