C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet719/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   715   716   717   718   719   720   721   722   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Name

 and 


InStock

, just as before. The compiler 

automatically “projects” the identifiers 

Name

 and 


InStock

, making them the property 

names of the anonymous type. Also as before, the properties are given the values specified 

by

item.Name

and

entry.InStock

.

Create a Group Join

As explained earlier, you can use 

into

 with 


join

 to create a 



group join,

 which creates a 

sequence in which each entry in the result consists of an entry from the first sequence and 

a group of all matching elements from the second sequence. No example was presented 

then because often a group join makes use of an anonymous type. Now that anonymous 

types have been covered, an example of a simple group join can be given.

The following example uses a group join to create a list in which various transports, such 

as cars, boats, and planes, are organized by their general transportation category, which is 

www.freepdf-books.com



572

 

P a r t   I :  



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

land, sea, and air. The program first creates a class called 



Transport

 that links a transport 

type with its classification. Inside 

Main( )

, it creates two input sequences. The first is an array 

of strings that contains the names of the general means by which one travels, which is land, 

sea, and air. The second is an array of 



Transport

, which encapsulates various means of 

transportation. It then uses a group join to produce a list of transports that are organized 

by their category.

// Demonstrate a simple group join.

using System;

using System.Linq;

// This class links the name of a transport, such as Train,

// with its general classification, such as land, sea, or air.

class Transport {

  public string Name { get; set; }

  public string How { get; set; }

  public Transport(string n, string h) {

    Name = n;

    How = h;

  }


}

class GroupJoinDemo {

  static void Main() {

    // An array of transport classifications.

    string[] travelTypes = {

         "Air",

         "Sea",

         "Land"

    };

    // An array of transports.



    Transport[] transports = {

         new Transport("Bicycle", "Land"),

         new Transport("Balloon", "Air"),

         new Transport("Boat", "Sea"),

         new Transport("Jet", "Air"),

         new Transport("Canoe", "Sea"),

         new Transport("Biplane", "Air"),

         new Transport("Car", "Land"),

         new Transport("Cargo Ship", "Sea"),

         new Transport("Train", "Land")

    };

    // Create a query that uses a group join to produce



    // a list of item names and IDs organized by category.

    var byHow = from how in travelTypes

                       join trans in transports

                       on how equals trans.How

                       into lst

                     select new { How = how, Tlist = lst };

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   715   716   717   718   719   720   721   722   ...   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