This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


■ A N   I N T R O D U C T I O N  TO   L I N Q 466



Download 21,46 Mb.
Pdf ko'rish
bet919/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   915   916   917   918   919   920   921   922   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

A N   I N T R O D U C T I O N  TO   L I N Q



466

8849CH14.qxd  9/26/07  12:30 PM  Page 466




foreach (Car c in onlyBMWs)

{

Console.WriteLine(c.ToString());



}

}

As seen earlier in this chapter, when you are building a where clause, it is permissible to make



use of any valid C# operators to build complex expressions. For example, consider the following

query that only extracts out the BMWs going at least 100 mph:



// Get BMWs going at least 100 mph.

var onlyFastBMWs = from c in myCars



where c.Make == "BMW" && c.Speed >= 100

select c;

foreach (Car c in onlyFastBMWs)

{

Console.WriteLine("{0} is going {1} MPH", c.PetName, c.Speed);



}

Projecting New Data Types

It is also possible to project new forms of data from an existing data source. Let’s assume that you

wish to take the incoming Car[] parameter and obtain a result set that accounts only for the make

and color of each vehicle. To do so, you can define a select statement that dynamically yields new

types via C# 2008 



anonymous types. Recall from Chapter 13 that the compiler defines a read-only

property and a read-only backing field for each specified name, and also is kind enough to override

ToString(), GetHashCode(), and Equals():

var makesColors = from c in myCars select new {c.Make, c.Color};

foreach (var o in makesColors)

{

// Could also use Make and Color properties directly.



Console.WriteLine(o.ToString());

}

Figure 14-8 shows the output of each of these new queries.



Figure 14-8.

Enumerating over subsets

C H A P T E R   1 4  



A N   I N T R O D U C T I O N  TO   L I N Q



467

8849CH14.qxd  9/26/07  12:30 PM  Page 467




Reversing Result Sets

You can reverse the items within a result set quite simply using the generic Reverse() method of

the Enumerable type. For example, the following method selects all items from the incoming Car[]

parameter in reverse:

static void ReversedSelection(Car[] myCars)

{


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   915   916   917   918   919   920   921   922   ...   1830




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