C# 0 The Complete Reference



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

descending

 option, as shown here:

var posNums = from n in nums

              orderby n descending

              select n;

If you try this, you will see that the order of the values is reversed.

Although sorting on a single criterion is often what is needed, you can use 

orderby

 to 


sort on multiple items by using this form:

orderby 


sort-onA direction

,

sort-onB direction

,

sort-onC direction

, …


www.freepdf-books.com


554

 

P a r t   I :  



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

In this form



sort-onA

 is the item on which the primary sorting is done. Then, each group of 

equivalent items is sorted on 

sort-onB

, and each of those groups is sorted on 



sort-onC

, and so 

on. Thus, each subsequent 

sort-on

 specifies a “then by” item on which to sort. In all cases, 



direction

 is optional, defaulting to 



ascending

. Here is an example that uses three sort criteria 

to sort bank account information by last name, then by first name, and finally by account 

balance:


// Sort on multiple criteria with orderby.

using System;

using System.Linq;

class Account {

  public string FirstName { get; private set; }

  public string LastName { get; private set; }

  public double Balance { get; private set; }

  public string AccountNumber { get; private set; }

  public Account(string fn, string ln, string accnum, double b) {

    FirstName = fn;

    LastName = ln;

    AccountNumber = accnum;

    Balance = b;

  }


}

class OrderbyDemo {

  static void Main() {

    // Create some data.

    Account[] accounts = { new Account("Tom", "Smith", "132CK", 100.23),

                           new Account("Tom", "Smith", "132CD", 10000.00),

                           new Account("Ralph", "Jones", "436CD", 1923.85),

                           new Account("Ralph", "Jones", "454MM", 987.132),

                           new Account("Ted", "Krammer", "897CD", 3223.19),

                           new Account("Ralph", "Jones", "434CK", -123.32),

                           new Account("Sara", "Smith", "543MM", 5017.40),

                           new Account("Sara", "Smith", "547CD", 34955.79),

                           new Account("Sara", "Smith", "843CK", 345.00),

                           new Account("Albert", "Smith", "445CK", 213.67),

                           new Account("Betty", "Krammer","968MM",5146.67),

                           new Account("Carl", "Smith", "078CD", 15345.99),

                           new Account("Jenny", "Jones", "108CK", 10.98)

                         };

    // Create a query that obtains the accounts in sorted order.

    // Sorting first by last name, then within same last names sorting by

    // by first name, and finally by account balance.

    var accInfo = from acc in accounts

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

                  select acc;

www.freepdf-books.com



Download 4,07 Mb.

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