C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet689/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   685   686   687   688   689   690   691   692   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

n

 to be an 



int

:

var posNums = from int n in nums



  // ...

Of course, the explicit type specification is not needed here because all arrays are implicitly 

convertible to 

IEnumerable

, which enables the type of the range variable to be inferred.

The type of object returned by a query is an instance of 

IEnumerable

, where 


T

 is 


the type of the elements. Thus, the type of the query variable must be an instance of 

IEnumerable

. The value of 



T

 is determined by the type of the value specified by the 



select

 clause. In the case of the preceding examples, 



T

 is 


int

 because 



n

 is an 


int

. (As explained, 



n

 is an 


int

 because 



int

 is the type of elements stored in 



nums

.) Therefore, the query could 

have been written like this, with the type explicitly specified as 

IEnumerable

:

IEnumerable posNums = from n in nums



                           where n > 0

                           select n;

The key point is that the type of the item selected by 

select

 must agree with the type 

argument passed to 

IEnumerable

 used to declare the query variable. Often query 

variables use 

var

 rather than explicitly specifying the type because this lets the compiler 

infer the proper type from the 

select

 clause. As you will see, this approach is particularly 

useful when 

select

 returns something other than an individual element from the data source.

When a query is executed by the 

foreach

 loop, the type of the iteration variable must be 

the same as the type of the range variable. In the preceding examples, this type was explicitly 

specified as 



int

, but you can let the compiler infer the type by specifying this variable as 



var

.

As you will see, there are also some cases in which 



var

 must be used because the data type 

has no name.

The General Form of a Query

All queries share a general form, which is based on a set of contextual keywords, shown here:

ascending

descending

equals

from


group

in

into



join

let


on

orderby


select

where


Of these, the following begin query clauses:

from


group

join


let

orderby


select

where


A query must begin with the keyword 

from

 and end with either a 



select

 or 


group

 clause. 

The

select

 clause determines what type of value is enumerated by the query. The 



group

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   685   686   687   688   689   690   691   692   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish