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

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

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




          aTicket。Numbers��2�� = numberToSearch OrElse _ 

          aTicket。Numbers��3�� = numberToSearch OrElse _ 

          aTicket。Numbers��4�� = numberToSearch OrElse _ 

          aTicket。Numbers��5�� = numberToSearch Then 

            runningTotal ��= 1 

        End If 

    Next 

    Return runningTotal 

End Function 



     Notice the similarity of the code to the code presented in Chapter 9。 The problem with this  

code is that you are iterating and solving a particular problem。 The code cannot be easily adapted  

to solving another problem。 

     The reusable code is in the form of a LINQ expression�此�



    Private Function FrequencyOfANumber��ByVal numberToSearch As Integer�� As Integer 

        Dim query = From ticket In _tickets _ 

                        Where ticket。Numbers��0�� = numberToSearch _ 

                        Or ticket。Numbers��1�� = numberToSearch _ 

                        Or ticket。Numbers��2�� = numberToSearch _ 

                        Or ticket。Numbers��3�� = numberToSearch _ 

                        Or ticket。Numbers��4�� = numberToSearch _ 

                        Or ticket。Numbers��5�� = numberToSearch _ 

                        Select ticket。Numbers 

        Return query。Count�┌� 

    End Function 



     The LINQ expression uses many constructs similar to a SQL SELECT statement。 Here are  

the basic rules of LINQ�此�



     o All LINQ queries must have a data source ��From��。 



     o All LINQ queries must have a filter ��Where���察�however�察�if the filter does not exist�察�an auto

       matic include´everything filter is implied。 



     o All LINQ queries must have a resulting dataset creator ��Select��。 



     To execute a LINQ expression�察�you need a data source。 The data source could be an object  

list�察�an XML document�察�or even a relational database table。 In the example�察�the data source is  

an object list and is defined using the From statement�此�



From ticket In _tickets 


´´´´´´´´´´´´´´´´´´´´´´Page 426´´´´´´´´´´´´´´´´´´´´´´´

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



                Looking at the From statement�察�you could get the idea that it is a  For Each statement without  

          the types。 Indeed�察�the iteration happens only when you actually try to use the result of the LINQ  

          query。 The From statement is saying to iterate the data source and assign each element ��a Ticket�� to  

          the variable ticket。 Note�察�however�察�that there is no explicit type information�察�which is one of  

          the strengths of LINQ!you have the ability to easily slice and dice data to suit your needs。 

               As you retrieve each item�察�you want to verify whether the item matches your needs。 If you  

          look at the code that isn¨t reusable�察�you¨ll see that it checks this with an If statement。 In LINQ�察 �

          you use the Where statement�察�which is similar to its SQL equivalent。 With the Where statement�察 �

          you test to see if the item matches your criteria。 In our case�察�we check each number in the  

          Ticket instance to see if it matches the number we¨re currently seeking。 

                If the Where returns True�察�we have a match and we will want to do something。 In the code  

          that isn¨t reusable�察�that means incrementing the runningTotal integer。 In LINQ�察�the aim is to  

          filter the data source ��_tickets in our case���察�and thus the  Select statement is used to create a  

          new result set of drawn numbers。 This result set contains all of the draws with the number we¨re  

          looking for ��numberToSearch���察�and if the draws are counted�察�we can get the frequency of that  

          number�察�which we then return。 

                Let¨s look at the LINQ that could be used to find the frequency of two numbers being drawn。 



           Function FrequencyOfTwoNumbers��ByVal number1ToSearch As Integer�察�_ 

                                             ByVal number2ToSearch As Integer�� As Integer 

              Dim query = From ticket2 In _ 

                              ��From ticket In _tickets _ 

                              Where ticket。Numbers��0�� = number1ToSearch  _ 

                               Or ticket。Numbers��1�� = number1ToSearch _ 

                               Or ticket。Numbers��2�� = number1ToSearch   _ 

                               Or ticket。Numbers��3�� = number1ToSearch _ 

                               Or ticket。Numbers��4�� = number1ToSearch  _ 

                               Or ticket。Numbers��5�� = number1ToSearch _ 

                              Select ticket�� _ 

                          Where ticket2。Numbers��0�� = number2ToSearch _ 

                          Or ticket2。Numbers��1�� = number2ToSearch _ 

                          Or ticket2。Numbers��2�� = number2ToSearch  _ 

                          Or ticket2。Numbers��3�� = number2ToSearch _ 

                          Or ticket2。Numbers��4�� = number2ToSearch  _ 

                          Or ticket2。Numbers��5�� = number2ToSearch _ 

                          Select ticket2。Numbers 

              Return query。Count�┌� 

           End Function 



                The LINQ statement is a concatenation of two LINQ queries�察�where one LINQ query is  

          bolded。 When the query is executed�察�the embedded query is executed and generates a result  

          set。 The result set is a data source on which the outer and second query operates�察�which then  

          generates another result set。 

               You do not need to embed LINQ queries as in the preceding code。 You could write functions  

          and embed the result of a LINQ query as the data source of another LINQ query。 The power of  

          LINQ is that you can�察�in theory�察�arbitrarily embed many queries within other queries�察�since you  

          are creating a filtering mechanism where one result set is the data source of another query。 


´´´´´´´´´´´´´´´´´´´´´´Page 427´´´´´´´´´´´´´´´´´´´´´´´

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



*Note  LINQ¨s strength is in its ability to slice and dice data to find the information that you want ��which is  

easy because it is data source�Cagnostic��。 LINQ requires more resources than similar Visual Basic code in  

longhand format。 But the benefit you get with LINQ is reusable code that you can maintain。 



     In the preceding section�察�we used LINQ to solve the frequency problem in a manner that  

promoted reusability。 For example�察�if you wanted to find out more statistics of the lottery draws�察�all  

you would need to do is write more LINQ statements that sliced and diced the existing list  

of lottery draws。 It would require adding only the method calls to the IExtendedProcessor。 

Destroy�┌� method。 However�察�let¨s consider the problem solved and think about what else can  

be done with LINQ。  



Learning More LINQ Tricks 



LINQ is not the only way to filter data。 Associated with LINQ are a number of extension methods  

that can be applied to lists。 For example�察�to filter for the frequency of a particular number�察�the  

following code could also have been used。 



    Function FrequencyOfANumber��ByVal numberToSearch As Integer�� As Integer 

        Dim query = _tickets。Where�─�_ 

                                   Function��ticket�察�index�� _ 

                ticket。Numbers��0�� = numberToSearch _ 

                Or ticket。Numbers��1�� = numberToSearch _ 

                Or ticket。Numbers��2�� = numberToSearch _ 

                Or ticket。Numbers��3�� = numberToSearch _ 

                Or ticket。Numbers��4�� = numberToSearch _ 

                Or ticket。Numbers��5�� = numberToSearch�� 

        Return query。Count�┌� 

    End Function 



     The ideas of LINQ that include From�察�Where�察�and Select are not lost�察�they just have not been  

used。 The From part is the _tickets variable itself。 The Where part is the method Where�┌��察�and the  

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