C# 0 The Complete Reference


where  clauses: where n > 0 where n < 10 The first  where



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

where

 clauses:

where n > 0

where n < 10

The first 

where

 requires that an element be greater than zero. The second requires the 

element to be less than 10. Thus, an element must be between 1 and 9 (inclusive) to satisfy 

both clauses.

www.freepdf-books.com



552

 

P a r t   I :  



T h e   C #   L a n g u a g e

Although it is not wrong to use two 



where

 clauses as just shown, the same effect can be 

achieved in a more compact manner by using a single 

where

 in which both tests are combined 

into a single expression. Here is the query rewritten to use this approach:

var posNums = from n in nums

              where n > 0 && n < 10

              select n;

In general, a 

where

 condition can use any valid C# expression that evaluates to a 

Boolean result. For example, the following program defines an array of 

string

s. Several of 

the strings define Internet addresses. The query 

netAddrs

 retrieves only those strings that 

have more than four characters and that end with “.net”. Thus, it finds those strings that 

contain Internet addresses that use the 



.net

 top-level domain name.

// Demonstrate another where clause.

using System;

using System.Linq;

class WhereDemo2 {

  static void Main() {

    string[] strs = { ".com", ".net", "hsNameA.com", "hsNameB.net",

                    "test", ".network", "hsNameC.net", "hsNameD.com" };

    // Create a query that obtains Internet addresses that

    // end with .net.

    var netAddrs = from addr in strs

                   where addr.Length > 4 && addr.EndsWith(".net")

                   select addr;

    // Execute the query and display the results.

    foreach(var str in netAddrs) Console.WriteLine(str);

  }

}

The output is shown here:



hsNameB.net

hsNameC.net

Notice that the program makes use of one of 

string

’s methods called 



EndsWith( )

 within the 



where

 clause. It returns true if the invoking string ends with the character sequence specified 

as an argument.

Sort Results with orderby

Often you will want the results of a query to be sorted. For example, you might want to 

obtain a list of past-due accounts, in order of the remaining balance, from greatest to least. 

Or, you might want to obtain a customer list, alphabetized by name. Whatever the purpose, 

LINQ gives you an easy way to produce sorted results: the 

orderby

 clause.


www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   687   688   689   690   691   692   693   694   ...   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