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

CHAPTER 21 INTRODUCTION TO LINQ

The from Clause

You saw that a query expression starts with a required from clause, which is followed by the query body. The body itself can start with any number of additional from clauses, where each subsequent from clause specifies an additional source data collection and introduces a new iteration variable for use in further evaluations. The syntax and meanings of all the from clauses are the same.

The following code shows an example of this use.

The first from clause is the required clause of the query expression.

The second from clause is the first clause of the query body.

The select clause creates objects of an anonymous type.

static

void Main()

 

 

 

{

 

 

 

 

 

var

groupA =

new[] { 3, 4, 5,

6

};

var

groupB =

new[] { 6, 7, 8,

9

};

var

someInts

= from a in

groupA

← Required first from clause

 

 

from b in

groupB

← First clause of query body

 

 

where a >

4 &&

b <= 8

 

 

select new {a,

b, sum = a + b}; ← Object of anonymous type

foreach (var

a in someInts)

 

 

 

Console.WriteLine(a);

 

 

 

}

 

 

 

 

 

This code produces the following output:

{a = 5, b = 6, sum = 11 }

{a = 5, b = 7, sum = 12 }

{a = 5, b = 8, sum = 13 }

{a = 6, b = 6, sum = 12 }

{a = 6, b = 7, sum = 13 }

{a = 6, b = 8, sum = 14 }

554

CHAPTER 21 INTRODUCTION TO LINQ

The let Clause

The let clause takes the evaluation of an expression and assigns it to an identifier to be used in other evaluations. The syntax of the let clause is the following:

let Identifier = Expression

For example, the query expression in the following code pairs each member of array groupA with each element of array groupB. The where clause eliminates each set of integers from the two arrays where the sum of the two is not equal to 12.

static void Main()

{

var groupA = new[] { 3, 4, 5, 6 }; var groupB = new[] { 6, 7, 8, 9 };

var someInts = from a

in

groupA

 

from b

in

groupB

← Store result in new variable

let sum =

a + b

where sum

== 12

 

select

new {a, b, sum};

 

foreach (var a in someInts) Console.WriteLine(a);

}

This code produces the following output:

{a = 3, b = 9, sum = 12 }

{a = 4, b = 8, sum = 12 }

{a = 5, b = 7, sum = 12 }

{a = 6, b = 6, sum = 12 }

555

CHAPTER 21 INTRODUCTION TO LINQ

The where Clause

The where clause eliminates items from further consideration if they don’t meet the specified condition. The syntax of the where clause is the following:

where BooleanExpression

Important things to know about the where clause are the following:

A query expression can have any number of where clauses, as long as they are in the from...let...where section.

An item must satisfy all the where clauses to avoid elimination from further consideration.

The following code shows an example of a query expression that contains two where clauses. The where clauses eliminate each set of integers from the two arrays where the sum of the two is not greater than or equal to 11, and the element from groupA is not the value 4. Each set of elements selected must satisfy the conditions of both where clauses.

static void Main()

{

var groupA = new[] { 3, 4, 5, 6 }; var groupB = new[] { 6, 7, 8, 9 };

var someInts = from int a in

groupA

from int b in

groupB

let sum = a +

b

where

sum >=

11

← Condition 1

where

a ==

4

 

← Condition 2

select new

{a, b, sum};

 

foreach (var a in someInts) Console.WriteLine(a);

}

This code produces the following output:

{a = 4, b = 7, sum = 11 }

{a = 4, b = 8, sum = 12 }

{a = 4, b = 9, sum = 13 }

556

CHAPTER 21 INTRODUCTION TO LINQ

The orderby Clause

The orderby clause takes an expression and returns the result items in order according to the expression. Figure 21-9 shows the syntax of the orderby clause. The optional keywords ascending and

descending set the direction of the order. Expression is generally a field of the items.

The default ordering of an orderby clause is ascending. You can, however, explicitly set the ordering of the elements to either ascending or descending, using the ascending and descending keywords.

There can be any number of orderby clauses, and they must be separated by commas.

Figure 21-9. The syntax of the orderby clause

The following code shows an example of student records ordered by the ages of the students. Notice that the array of student information is stored in an array of anonymous types.

static

void Main(

) {

 

 

var

students =

new

[]

// Array of objects of an anonymous type

{

 

 

 

 

new { LName="Jones",

FName="Mary",

new

{

LName="Smith",

FName="Bob",

new

{

LName="Fleming",

FName="Carol",

};

Age=19, Major="History" }, Age=20, Major="CompSci" }, Age=21, Major="History" }

var query = from student in students

← Order by Age.

orderby student.Age

select student;

 

foreach (var s in query) {

 

Console.WriteLine("{0}, {1}: {2}

- {3}",

s.LName, s.FName, s.Age,

s.Major);

}

 

}

This code produces the following output:

Jones, Mary: 19 - History

Smith, Bob: 20 - CompSci

Fleming, Carol: 21 - History

557

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