Tải bản đầy đủ - 0 (trang)
Query Expression Syntax vs. Standard Dot Notation Syntax

Query Expression Syntax vs. Standard Dot Notation Syntax

Tải bản đầy đủ - 0trang

C H A P T E R 3 N LI N Q T O O B JE C T S I N T R O D U C T I O N



87



)))))))))

.

/

Notice the results do not change from one enumeration to the next. This is because the

PkHeop method is not deferred, and the query is actually performed at the time the query is

called.

To return to a technical discussion of what is different between this example and Listing 3-4, while the Oaha_p operator is still deferred in Listing 3-5, the PkHeop operator is not.

When the PkHeop operator is called in the query statement, it enumerates the object returned

from the Oaha_p operator immediately, making the entire query not deferred.



Func Delegates

Several of the Standard Query Operators are prototyped to take a Bqj_ delegate as an argument. This prevents you from having to explicitly declare delegate types. Here are the Bqj_

delegate declarations:

Lq^he_@ahac]paBqj_pekjBqj_$KbPNaoqhp%$%=oPNaoqhp

Lq^he_@ahac]paBqj_pekjBqj_$KbP-(PNaoqhp%$>uR]h]nc-=oP-%=oPNaoqhp

Lq^he_@ahac]paBqj_pekjBqj_$KbP-(P.(PNaoqhp%[

$>uR]h]nc-=oP-(>uR]h]nc.=oP.%=oPNaoqhp

Lq^he_@ahac]paBqj_pekjBqj_$KbP-(P.(P/(PNaoqhp%[

$>uR]h]nc-=oP-(>uR]h]nc.=oP.(>uR]h]nc/=oP/%=oPNaoqhp

Lq^he_@ahac]paBqj_pekjBqj_$KbP-(P.(P/(P0(PNaoqhp%[

$>uR]h]nc-=oP-(>uR]h]nc.=oP.(>uR]h]nc/=oP/([

>uR]h]nc0=oP0%=oPNaoqhp

In each declaration, PNaoqhp refers to the data type returned. Notice that the return type

argument, PNaoqhp, is at the end of the parameter type template for every overload of the Bqj_

delegate. The other type parameters, P-, P., P/, and P0, refer to the input parameters passed

to the method. The multiple declarations exist because some Standard Query Operators have

delegate arguments that require more parameters than others. By looking at the declarations,

you can see that no Standard Query Operator has a delegate argument that will require more

than four input parameters.

Let’s take a look at one of the declarations of the Sdana operator:

8Atpajoekj=ppne^qpa:[

Lq^he_Od]na`Bqj_pekjSdana$KbPOkqn_a%$[

okqn_a=oEAjqian]^ha$KbPOkqn_a%([

lna`e_]pa=oBqj_$KbPOkqn_a(>kkha]j%%[

=oEAjqian]^ha$KbPOkqn_a%



www.it-ebooks.info

16445ch03_CMP3.indd 87



7/10/09 11:40:19 AM



88



CH APT ER 3 N L INQ TO OB J EC TS INTR ODU C T I O N



The predicate argument is specified as a Bqj_$KbPOkqn_a(>kkha]j%. From this, you can

see the predicate method or lambda expression must accept a single argument, the POkqn_a

parameter, and return a >kkha]j. You know this because you know the return type is specified

at the end of the parameter template list.

Of course, you can use the Bqj_ declaration, as shown in Listing 3-6.

Listing 3-6. An Example Using One of the Func Delegate Declarations

#?na]pa]j]nn]ukbEjpacano*

@eiejpo$%=oEjpacan9w-(.(/(0(1(2y

#@a_h]nakqn`ahac]pa*

@eiCna]panPd]jPsk=oBqj_$KbEjpacan(>kkha]j%9Bqj_pekj$e%e:.

#Lanbknipdamqanu***jkpna]hhu*@kj#pbkncap]^kqp`abanna`mqaneao

@eiejpoCna]panPd]jPsk=oEAjqian]^ha$KbEjpacan%9ejpo*Sdana$Cna]panPd]jPsk%

#@eolh]updanaoqhpo*

BknA]_de=oEjpacanEjejpoCna]panPd]jPsk

?kjokha*SnepaHeja$e%

Jatpe

This code provides the following results:

/

0

1

2



The Standard Query Operators Alphabetical

Cross-Reference

Table 3-1 shows the Standard Query Operators listed alphabetically. Since these operators will

be separated into chapters based on whether they are deferred or not, this table will help you

locate each operator in the remaining LINQ to Objects chapters. Note that =ccnac]pa, @eopej_p,

Ogel, OgelSdeha, P]ga, and P]gaSdeha have query expression support in VB.NET only, and not

in C#.

Table 3-1. Standard Query Operators Alphabetical Cross-Reference



Operator



Purpose



=ccnac]pa



Aggregate



=hh



Quantifiers



=ju



Quantifiers



Deferred?



Query Expression Support?





www.it-ebooks.info

16445ch03_CMP3.indd 88



7/10/09 11:40:19 AM



C H A P T E R 3 N LI N Q T O O B JE C T S I N T R O D U C T I O N



Operator



Purpose



Deferred?



=oAjqian]^ha



Conversion



=ran]ca



Aggregate



?]op



Conversion







?kj_]p



Concatenation







?kjp]ejo



Quantifiers



?kqjp



Aggregate



@ab]qhpEbAilpu



Element







@eopej_p



Set







Ahaiajp=p



Element



Ahaiajp=pKn@ab]qhp



Element



Ailpu



Generation







At_alp



Set







Benop



Element



BenopKn@ab]qhp



Element



Cnkql>u



Grouping











CnkqlFkej



Join











Ejpanoa_p



Set







Fkej



Join







H]op



Element



H]opKn@ab]qhp



Element



Hkjc?kqjp



Aggregate



I]t



Aggregate



Iej



Aggregate



KbPula



Conversion







Kn`an>u



Ordering











Kn`an>u@ao_aj`ejc



Ordering











N]jca



Generation







Nala]p



Generation







Naranoa



Ordering







Oaha_p



Projection











Oaha_pI]ju



Projection











Oamqaj_aAmq]h



Equality



Oejcha



Element



OejchaKn@ab]qhp



Element



89



Query Expression Support?















Continued



www.it-ebooks.info

16445ch03_CMP3.indd 89



7/10/09 11:40:19 AM



90



CH APT ER 3 N L INQ TO OB J EC TS INTR ODU C T I O N



Table 3-1. Continued



Operator



Purpose



Deferred?



Query Expression Support?



Ogel



Partitioning











OgelSdeha



Partitioning











Oqi



Aggregate



P]ga



Partitioning











P]gaSdeha



Partitioning











Pdaj>u



Ordering











Pdaj>u@ao_aj`ejc



Ordering











Pk=nn]u



Conversion



Pk@e_pekj]nu



Conversion



PkHeop



Conversion



PkHkkgql



Conversion



Qjekj



Set







Sdana



Restriction











A Tale of Two Syntaxes

Since you may write LINQ queries using either query expression syntax or standard dot notation syntax, you may wonder which syntax you should use. In many cases, this is largely a

matter of preference as long as the standard query operators you are using in your query are

supported by query expression syntax. As you can see in the previous table, most of the operators are not supported by query expression syntax, so when using any of the unsupported

operators, you must defer to standard dot notation syntax.

However, you should be aware that you can use a mixture of both syntaxes by enclosing

a query expression inside parentheses and appending a call to an unsupported operator like

this:

@eik``Jqi^ano9$BnkijEjjqio[

SdanajIk`.9-[

Oaha_pj%[

*Naranoa$%



Summary

In this chapter, I introduced you to the term sequence, and its technical data type,

EAjqian]^ha$KbP%. If you feel uncomfortable with some of this terminology, I am sure

that with time, it will become second nature for you. Just think of EAjqian]^ha$KbP% as a

sequence of objects you are going to call methods on to do things with those objects.



www.it-ebooks.info

16445ch03_CMP3.indd 90



7/10/09 11:40:19 AM



C H A P T E R 3 N LI N Q T O O B JE C T S I N T R O D U C T I O N



91



However, if there is one thing I want you to take with you from this chapter, it is the

importance of deferred query execution. It can work for you or against you. Understanding it is

key, and being conscious of it is important. It is so important that I have divided the Standard

Query Operators into separate chapters based on this characteristic. The deferred operators

are covered in Chapter 4, and the nondeferred operators are covered in Chapter 5.

Since I have deferred queries in your thoughts right now, I will begin an in-depth examination of the deferred operators in the next chapter.



www.it-ebooks.info

16445ch03_CMP3.indd 91



7/10/09 11:40:19 AM



www.it-ebooks.info

16445ch03_CMP3.indd 92



7/10/09 11:40:19 AM



CHAP TER



4



Deferred Operators

I



n the previous chapter, I covered what sequences are, the data type that represents them,

and the impact of deferred query execution. Because of the importance of deferred query

operator awareness, I have separated deferred and nondeferred operators into separate chapters to highlight whether a Standard Query Operator’s action is deferred or not.

In this chapter, I will be covering the deferred query operators. A deferred operator is easy

to spot because it has a return type of EAjqian]^ha$KbP% or EKn`ana`Ajqian]^ha$KbP%. Each

of these deferred operators will be categorized by its purpose.

In order to code and execute the examples in this chapter, you will need to make sure you

have Eilknpo statements for all the necessary namespaces, references for all the necessary

assemblies, and the common code that the examples will share.



Referenced Namespaces

The examples in this chapter will use the Ouopai*Hejm, Ouopai*?khha_pekjo, Ouopai*

?khha_pekjo*Cajane_, and Ouopai*@]p]*Hejm namespaces. Therefore, you should add the

following Eilknpo directives to your code if they are not present, or included in your project

settings:

EilknpoOuopai*Hejm

EilknpoOuopai*?khha_pekjo

EilknpoOuopai*?khha_pekjo*Cajane_

EilknpoOuopai*@]p]*Hejm



Referenced Assemblies

In addition to the typical assemblies, you will need references for the Ouopai*@]p]*Hejm*`hh

assembly.



93



www.it-ebooks.info

16445ch04_CMP2.indd 93



7/9/09 12:44:33 PM



94



CH APT ER 4 N DEFER R ED OP ER A TOR S



Query Expression Syntax vs. Standard Dot

Notation Syntax

For most of the examples in this chapter, and indeed in this book, I use standard dot notation syntax for my queries. While any LINQ query can be written using standard dot notation

syntax, just so you will have some examples using query expression syntax, I will use query

expression syntax for a few examples in this chapter. In particular, I will be using query expression syntax in the examples of operators for which VB.NET supports query expression syntax

but C# does not: P]ga (Listing 4-10), P]gaSdeha (Listing 4-12), Ogel (Listing 4-14), OgelSdeha

(Listing 4-15), and @eopej_p (Listing 4-34). Be aware that all of these could have been done

with standard dot notation syntax, and that some of the other examples could have been done

with query expression syntax.



Common Classes

Several of the examples in this chapter will require classes to fully demonstrate an operator’s

behavior. The following is a list of classes that will be shared by more than one example. The

Ailhkuaa class is meant to represent an employee. For convenience, it contains shared methods to return an =nn]uHeop or array of employees.

The Shared Employee Class

Lq^he_?h]ooAilhkuaa

Lq^he_e`=oEjpacan

Lq^he_benopJ]ia=oOpnejc

Lq^he_h]opJ]ia=oOpnejc

Lq^he_Od]na`Bqj_pekjCapAilhkuaao=nn]uHeop$%=o=nn]uHeop

@ei]h=oJas=nn]uHeop$%

]h*=``$JasAilhkuaaSepd[

w*e`9-(*benopJ]ia9Fka(*h]opJ]ia9N]ppvy%

]h*=``$JasAilhkuaaSepd[

w*e`9.(*benopJ]ia9Sehhe]i(*h]opJ]ia9C]paoy%

]h*=``$JasAilhkuaaSepd[

w*e`9/(*benopJ]ia9=j`ano(*h]opJ]ia9Dafho^ancy%

]h*=``$JasAilhkuaaSepd[

w*e`90(*benopJ]ia9@]re`(*h]opJ]ia9Hecdpi]jy%

]h*=``$JasAilhkuaaSepd[

w*e`9-,-(*benopJ]ia9Garej(*h]opJ]ia9Bhujjy%

Napqnj$]h%

Aj`Bqj_pekj

Lq^he_Od]na`Bqj_pekjCapAilhkuaao=nn]u$%=oAilhkuaa$%

Napqnj$CapAilhkuaao=nn]uHeop$%*Pk=nn]u$CapPula$Ailhkuaa%%%

Aj`Bqj_pekj

Aj`?h]oo



www.it-ebooks.info

16445ch04_CMP2.indd 94



7/9/09 12:44:33 PM



C H A P T E R 4 N D E F E R R E D O P E R A T O R S



95



The AilhkuaaKlpekjAjpnu class represents an award of stock options to a specific

employee. For convenience, it contains a shared method to return an array of awarded option

entries.

The Shared EmployeeOptionEntry Class

Lq^he_?h]ooAilhkuaaKlpekjAjpnu

Lq^he_e`=oEjpacan

Lq^he_klpekjo?kqjp=oHkjc

Lq^he_`]pa=s]n`a`=o@]paPeia

Lq^he_Od]na`Bqj_pekjCapAilhkuaaKlpekjAjpneao$%=oAilhkuaaKlpekjAjpnu$%

@eiailKlpekjo$%=oAilhkuaaKlpekjAjpnu9w[

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9-([

*klpekjo?kqjp9.([

*`]pa=s]n`a`9@]paPeia*L]noa$-555+-.+/-%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9.([

*klpekjo?kqjp9-,,,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-55.+,2+/,%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9.([

*klpekjo?kqjp9-,,,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-550+,-+,-%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9/([

*klpekjo?kqjp91,,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-553+,5+/,%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9.([

*klpekjo?kqjp9-,,,,([

*`]pa=s]n`a`9@]paPeia*L]noa$.,,/+,0+,-%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9/([

*klpekjo?kqjp931,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-554+,5+/,%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9/([

*klpekjo?kqjp931,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-554+,5+/,%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`90([

*klpekjo?kqjp9-1,,([

*`]pa=s]n`a`9@]paPeia*L]noa$-553+-.+/-%y([

JasAilhkuaaKlpekjAjpnuSepdw[

*e`9-,-([

*klpekjo?kqjp9.([



www.it-ebooks.info

16445ch04_CMP2.indd 95



7/9/09 12:44:33 PM



96



CH APT ER 4 N DEFER R ED OP ER A TOR S



*`]pa=s]n`a`9@]paPeia*L]noa$-554+-.+/-%y[

y

Napqnj$ailKlpekjo%

Aj`Bqj_pekj

Aj`?h]oo



The Deferred Operators by Purpose

The deferred Standard Query Operators are organized by their purpose in this section.



Restriction

Restriction operators are used for including or excluding elements of an input sequence.



Where

The Sdana operator is used to filter elements into a sequence.

Declarations

The Sdana operator has two overloads.



NNote The first overload shows how the .NET Framework defines the Sdana operator. The

8Atpajoekj=ppne^qpa: is used internally by the Microsoft .NET Framework and should not be used by

any application code. It is not related to the 8Atpajoekj$%: attribute, which is meant to be used in applica-



tions. Also note that this is not a case of an evil Microsoft using secret functionality to gain an advantage over

competitors. Frameworks typically need flags and functions for internal housekeeping, and this is just one of

those occasions. Even though it is only meant for internal use, it is included in the .NET documentation.



The First Overload of the Where Operator

8Atpajoekj=ppne^qpa:[

Lq^he_Od]na`Bqj_pekjSdana$KbPOkqn_a%$[

okqn_a=oEAjqian]^ha$KbPOkqn_a%([

lna`e_]pa=oBqj_$KbPOkqn_a(>kkha]j%[

%=oEAjqian]^ha$KbPOkqn_a%

This overload of Sdana takes an input source sequence and a lna`e_]pa delegate and

returns an object that, when enumerated, enumerates through the input source sequence

yielding elements for which the lna`e_]pa delegate returns Pnqa.

Because this is an extension method, we do not actually pass the input sequence, as long

as we call the Sdana operator using the instance method syntax.



www.it-ebooks.info

16445ch04_CMP2.indd 96



7/9/09 12:44:33 PM



C H A P T E R 4 N D E F E R R E D O P E R A T O R S



97



NNote Thanks to extension methods, it is not necessary to pass the first argument to the Standard Query

Operators that specify the 8Atpajoekj=ppne^qpa: attribute, as long as we call the operator on an object of

the same type as the first argument of the method.



When calling Sdana, you pass a delegate to a lna`e_]pa method. Your lna`e_]pa method

must accept a type POkqn_a as input, where POkqna is the type of elements contained in the

input sequence, and return a >kkha]j. The Sdana operator will call your lna`e_]pa method for

each element in the input sequence and pass it the element. If your lna`e_]pa method returns

Pnqa, Sdana will yield that element into Sdana’s output sequence. If your lna`e_]pa method

returns B]hoa, it will not.

The Second Overload of the Where Operator

8Atpajoekj=ppne^qpa:[

Lq^he_Od]na`Bqj_pekjSdana$KbPOkqn_a%$[

okqn_a=oEAjqian]^ha$KbPOkqn_a%([

lna`e_]pa=oBqj_$KbPOkqn_a(Ejpacan(>kkha]j%[

%=oEAjqian]^ha$KbPOkqn_a%

The second Sdana overload is identical to the first one, except it specifies that your

lna`e_]pa delegate receives an additional integer input argument. That argument will be the

index number for the element from the input sequence.

The index is zero based, so the index passed for the first element will be zero. The last element will be passed the total number of elements in the sequence minus one.



NNote Remember, the index that gets passed will be zero based.



Exceptions

=ncqiajpJqhhAt_alpekj is thrown if any of the arguments are Jkpdejc.

Examples

Listing 4-1 is an example calling the first overload.

Listing 4-1. An Example of the First Where Overload

@eilnaoe`ajpo$%=oOpnejc9w[

=`]io(=npdqn(>q_d]j]j(>qod(?]npan(?harah]j`([

?hejpkj(?kkhe`ca(Aeoajdksan(Behhikna(Bkn`(C]nbeah`([

Cn]jp(D]n`ejc(D]nneokj(D]uao(Dkkran(F]_gokj([

Fabbanokj(Fkdjokj(Gajja`u(Hej_khj(I]`eokj(I_Gejhau([



www.it-ebooks.info

16445ch04_CMP2.indd 97



7/9/09 12:44:33 PM



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Query Expression Syntax vs. Standard Dot Notation Syntax

Tải bản đầy đủ ngay(0 tr)

×