C# 0 The Complete Reference


PART I C h a p t e r   1 9



Download 4,07 Mb.
Pdf ko'rish
bet695/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   691   692   693   694   695   696   697   698   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 9 :  

L I N Q  

555


PART IPART I

    Console.WriteLine("Accounts in sorted order: ");

    string str = "";

    // Execute the query and display the results.

    foreach(Account acc in accInfo) {

      if(str != acc.FirstName) {

        Console.WriteLine();

        str = acc.FirstName;

      }

      Console.WriteLine("{0}, {1}\tAcc#: {2}, {3,10:C}",

                        acc.LastName, acc.FirstName,

                        acc.AccountNumber, acc.Balance);

    }

    Console.WriteLine();



  }

}

The output is shown here:



Accounts in sorted order:

Jones, Jenny       Acc#: 108CK,     $10.98

Jones, Ralph       Acc#: 434CK,  ($123.32)

Jones, Ralph       Acc#: 454MM,    $987.13

Jones, Ralph       Acc#: 436CD,  $1,923.85

Krammer, Betty     Acc#: 968MM,  $5,146.67

Krammer, Ted       Acc#: 897CD,  $3,223.19

Smith, Albert      Acc#: 445CK,  ($213.67)

Smith, Carl        Acc#: 078CD, $15,345.99

Smith, Sara        Acc#: 843CK,    $345.00

Smith, Sara        Acc#: 543MM,  $5,017.40

Smith, Sara        Acc#: 547CD, $34,955.79

Smith, Tom         Acc#: 132CK,    $100.23

Smith, Tom         Acc#: 132CD, $10,000.00

In the query, look closely at how the 

orderby

 clause is written:

var accInfo = from acc in accounts

              orderby acc.LastName, acc.FirstName, acc.Balance

              select acc;

Here is how it works. First, the results are sorted by last name, and then entries with the 

same last name are sorted by the first name. Finally, groups of entries with the same first 

www.freepdf-books.com




556

 

P a r t   I :  



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

and last name are sorted by the account balance. This is why the list of accounts under the 

name Jones is shown in this order:

Jones, Jenny       Acc#: 108CK,     $10.98

Jones, Ralph       Acc#: 434CK,  ($123.32)

Jones, Ralph       Acc#: 454MM,    $987.13

Jones, Ralph       Acc#: 436CD,  $1,923.85

As the output confirms, the list is sorted by last name, then by first name, and finally by 

account balance.

When using multiple criteria, you can reverse the condition of any sort by applying 

the

descending

 option. For example, this query causes the results to be shown in order 

of decreasing balance:

var accInfo = from acc in accounts

              orderby x.LastName, x.FirstName, x.Balance descending

              select acc;

When using this version, the list of Jones entries will be displayed like this:

Jones, Jenny       Acc#: 108CK,     $10.98

Jones, Ralph       Acc#: 436CD,  $1,923.85

Jones, Ralph       Acc#: 454MM,    $987.13

Jones, Ralph       Acc#: 434CK,  ($123.32)

As you can see, now the accounts for Ralph Jones are displayed from greatest to least.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   691   692   693   694   695   696   697   698   ...   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