嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及144嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��






Max�┌�                 Finds the maximum value of a list。 



Min�┌�                 Finds the minimum value of a list。 



Reverse�┌�             Reverses the order of the list。 



Select�┌�              Selects from the iteration being executed。 



SelectMany�┌�          Selects many items from a list where the selected items form another list。 



Sum�┌�                 Calculates the sum of a list。 



Union�┌�               Takes the list and the passed´in list and calculates the union of the two lists。 Uses  

                      the equality test as defined in the Distinct�┌� method。 


´´´´´´´´´´´´´´´´´´´´´´Page 430´´´´´´´´´´´´´´´´´´´´´´´

408       CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 



           *Note  With Visual Basic 2008�察�lists and the manipulation of lists have dramatically changed for the better。  

           The general structure is to define methods that allow a developer to specify a lambda expression that is then  

           chained together with other methods。 Take some time to learn about all of the possibilities。 



                As an example of the power of the various methods�察�consider the following code�察�which  

           pacts the frequency code into a couple of lines of source code。 



               Function FrequencyOfANumberFunc��ByVal numberToSearch As Integer�� As Integer 

                   Return _tickets。SelectMany�─�_ 

                     Function��ticket�� ticket。Numbers��。Where�─�_ 

                       Function��num�� num = numberToSearch��。Count�┌� 

               End Function 



                Here�察�each ticket is iterated by calling the SelectMany�┌� method。 This returns an array of  

           numbers�察�which represents the drawn numbers。 The purpose of SelectMany�┌� is to bine  

           the individual arrays of numbers into a large array of numbers。 The code then calls Where�┌�  

           to filter out only those numbers that equal the number to search for�察�and finally the Count�┌�  

           method is called to return the number of found values。 

                The following sections present examples of using the extension methods with LINQ。 They  

           are in the  LINQExamples project�察�which is a console application。  



           *Note  In all of the examples�察�I have taken shortcuts for simplicity。 So you will see some coding practices  

           that are not remended�察�such as creating public data members and not using properties。 



           Selecting and Altering Data 



          When running a LINQ query�察�the data that you filter and manipulate does not need to stay in  

           its original form。 Let¨s say that you have a list of customers�察�and you have identified a set of  

           customers who deserve more loyalty points。 You want to select those customers�察�increment  

           their points�察�and then return the list of altered customers。 To do that�察�you can mix LINQ with  

           the extension methods。 

                Consider the following simplified customer declaration。  



           Class Customer  

               Public Identifier As String 

               Public Points As Integer 

               Public Overrides Function ToString�┌� As String 

                   Return ;Identifier �──�& Identifier & ;�� Points �──�& Points & ;��; 

               End Function 

           End Class 



                A list will be created with two customers�察�where one customer has no points and the other  

           one does。 Here is the source code to create that list�此�


´´´´´´´´´´´´´´´´´´´´´´Page 431´´´´´´´´´´´´´´´´´´´´´´´

                                                                  CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 409 



    Private Function CreateList�┌� As Customer�┌� 

        Return New Customer�┌� �� _ 

            New Customer�┌� With ��。Identifier = ;Person 1;�察 �Points = 0���察�_ 

            New Customer�┌� With ��。Identifier = ;Person 2;�察 �Points = 10���� 

    End Function  



     The customers that have enough points are selected and rewarded with extra points。 To do  

that�察�use the following LINQ statement。 



    Function Increment��ByVal pCustomer As Customer�察�ByVal index As Integer�� _ 

      As Customer 

        pCustomer。Points ��= 5 

        Return pCustomer 

    End Function 



    Sub CountCustomers�┌� 

        Dim points = ��From customer In CreateList�┌� _ 

                      Where customer。Points 〃 5 _ 

                      Select customer��。Select��AddressOf Increment�� 

        Console。WriteLine�─�Count is �──�& points。Count�┌� & ;��;�� 

    End Sub 



     The LINQ query is bined with a modification operation。 The LINQ statement that uses  

From�察�Where�察�and Select is not new。 New are the parentheses enclosing the LINQ statement。 By  

using a set of parentheses�察�you are identifying the LINQ statement as an object that references  

a result set。  

     In the example�察�the method called on the LINQ statement is Select�┌�。 Using the Select�┌�  

method�察�each item in the result set is iterated and passed as a parameter ��pCustomer�� to the  

lambda expression ��Increment�┌���。 Passed with the item is the index of the item in the list。 The  

role of the lambda expression is to do something with the item and return what should be used as  

a basis for another list。 In the example�察�an instance of the type Customer is passed in to Increment�┌��察 �

and an instance of type Customer is passed out。 But before the instance is returned�察�it is manip

ulated to get an additional five bonus points。  

     What might concern you is that there is no test to check if the customer actually warrants  

earning five bonus points。 That would be a concern if you were not using the LINQ expression。  

The LINQ expression is responsible for filtering out only those customers who should get the  

additional bonus points。 Thus�察�when the method Select�┌� is called�察�you are 100�ァ�sure that  

only the customers who should get bonus points actually get bonus points。 Think of this as  

building a pipeline of manipulations。 



Selecting with Anonymous Types 



The Select�┌� method and statement are used to generate a result set after finding a particular  

element。 As demonstrated in the previous section�察�a Select statement is used to generate a new  

result set。 For example�察�what if you want to find all of the customers who fulfill a certain crite

rion�察�but do not want to copy all of the associated data�拭�You might want only the customer  

identifier and accumulated points。 To do that�察�you could modify the Select�┌� part of the LINQ  

statement to return a new type that you declare dynamically。 The following is the previous  

example rewritten to use an anonymous type。 


´´´´´´´´´´´´´´´´´´´´´´Page 432´´´´´´´´´´´´´´´´´´´´´´´

410       CH AP T E R   1 5   *    L E A R N I N G   A B OU T   L I N Q 



              Function Increment2��ByVal pCustomer As Customer�察�ByVal index As Integer�� 

                  pCustomer。Points ��= 5 

                  Return New With ��。identifier = pCustomer。Identifier�察�_ 

                        。points = pCustomer。Points�� 

              End Function 



              Sub CountCustomers2�┌� 

                  Dim points = ��From customer In CreateList�┌� _ 

                                Where customer。Points 〃 5 _ 

                                Select customer��。Select��AddressOf Increment2�� 

                  Console。WriteLine�─�Count is �──�& points。Count�┌� & ;��;�� 

              End Sub  



               In the example�察�the Return statement in the  Increment2�┌� method uses the keyword New  

          without a type identifier�察�but with the syntax of an object initializer。 This is defining an anony

          mous type。 An anonymous type is an object instance that has no explicitly named type。 The  

          anonymous type has properties�察�but it does not have methods。 

               Anonymous types are useful only in the context of the method in which they are declar
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��