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


// Assume we have an array of strings



Download 21,46 Mb.
Pdf ko'rish
bet885/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   881   882   883   884   885   886   887   888   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Assume we have an array of strings.

string[] currentVideoGames = {"Morrowind", "BioShock",

"Half Life 2: Episode 1", "The Darkness",

"Daxter", "System Shock 2"};

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



450

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




// Build a query expression to represent the items in the array

// that have more than 6 letters.

IEnumerable subset = from g in currentVideoGames

where g.Length > 6 orderby g select g;

// Print out the results.

foreach (string s in subset)

Console.WriteLine("Item: {0}", s);

}

Notice that the query expression created here makes use of the from, in, where, orderby,



and select LINQ query operators. We will dig into the formalities of query expression syntax in

just a bit, but even now you should be able to parse this statement as “Give me the items inside of

currentVideoGames that have more than six characters, ordered alphabetically.” Here, each item that

matches the search criteria has been given the name “g” (as in “game”); however, any valid C# vari-

able name would do:

IEnumerable subset = from game in currentVideoGames

where game.Length > 6 orderby game select game;

Notice that the “result set” variable, subset, is represented by an object that implements the

generic version of IEnumerable, where T is of type System.String (after all, we are querying an

array of strings). Once we obtain the result set, we then simply print out each item using a standard

foreach construct.

Before we see the results of our query, assume the Program class defines an additional helper

function named ReflectOverQueryResults() that will print out various details of the LINQ result set

(note the parameter is a System.Object, to account for multiple types of result sets):

static void ReflectOverQueryResults(object resultSet)

{

Console.WriteLine("***** Info about your query *****");



Console.WriteLine("resultSet is of type: {0}", resultSet.GetType().Name);

Console.WriteLine("resultSet location: {0}", resultSet.GetType().Assembly);

}

Assuming you have called this method within QueryOverStrings() directly after printing



out the obtained subset, if you run the application, you will see the subset is really an instance

of the generic OrderedEnumerable type (represented in terms of CIL code as

OrderedEnumerable`2), which is an internal abstract type residing in the System.Core.dll assembly

(see Figure 14-2).



Figure 14-2.

The result of our LINQ query

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



451

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





Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   881   882   883   884   885   886   887   888   ...   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