小牛电子书 > 其他电子书 > VB2008从入门到精通(PDF格式英文版) >

第146章

VB2008从入门到精通(PDF格式英文版)-第146章

小说: VB2008从入门到精通(PDF格式英文版) 字数: 每页3500字

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




            New Customer() With {。Identifier = 〃Person 2〃; 。Points = 10}} 



     To get a list of all unique customers; you can use Union(); as follows: 



Dim uniqueCustomers = customers1。Union(customers2) 



     Contained within the list represented by the variable uniqueCustomers will be the three  

customers of the two lists。 



Using LINQ in Other Contexts 



So far; all of the examples in this chapter involved using LINQ and objects。 However; LINQ is  

not just an object…searching technology。 It is also usable with XML and relational databases。  

Using LINQ with these other data sources is not a problem; since the querying is identical。  

What is a problem is getting the query to work in the first place。  

     Consider Figure 15…1; which illustrates the LINQ architecture。  

     As you can see in Figure 15…1; all  programming languages can access the LINQ  

library。 The data manipulated by the LINQ library es from what is called a LINQ…enabled  

data source。 The examples that you’ve seen use the LINQ to objects data source。 


…………………………………………………………Page 436……………………………………………………………

414       CH AP T E R   1 5   ■    L E A R N I N G   A B OU T   L I N Q 



           Figure 15…1。 LINQ architecture (based on an image in MSDN Magazine; http://msdn。microsoft。/ 

           msdnmag/issues/07/06/csharp30/default。aspx) 



                However; there is also the possibility to use a LINQ…enabled ADO connection。 The  

           good news is that you can use LINQ with a relational database。 The bad news is that the rela

           tional database’s ADO driver must support the special LINQ characteristics。 At the time  

           of this writing; only the Microsoft SQL Server driver supports LINQ。 Currently; the drivers for  

           Microsoft Access; MySQL; and other relational databases do not support LINQ。 

                Consider this LINQ query: 



           Dim northwind As NorthwindDataContext = _ 

             New NorthwindDataContext() 

           Dim products = From p In northwind。Products _ 

               Where p。OrderDetails。Count = 0 And p。UnitPrice 》 100 _ 

               Select p 



                Notice the code in the From statement。 The data source is an object that references the rela

           tional database Products table。 If a database driver is optimized for LINQ; it will understand the  

           LINQ query and optimize it as if it were a SQL statement。  

                If your database driver does not support LINQ; then you have a problem because; in theory;  

           you would need to download all the data from the table; and then execute the LINQ query。 That  

           would waste resources and is not remended。 


…………………………………………………………Page 437……………………………………………………………

                                                                  CH AP T E R   1 5   ■    L E A R N I N G   A B OU T   L I N Q 415 



■Note  For examples of LINQ using relational databases; see Beginning VB 2008 Databases by Vidya Vrat  

Agarwal and James Huddleston (Apress; 2008)。 



     Let’s say that you want to execute LINQ on an XML document。 Consider the following  

XML LINQ code (from  http://hookedonlinq。/LINQtoXML5MinuteOverview。ashx)。 



Dim loaded As XDocument = XDocument。Load(〃C:contacts。xml〃) 



 ' Query the data and write out a subset of contacts 

Dim q = From c In loaded。Descendants(〃contact〃) _ 

            Where CType(c。Attribute(〃contactId〃)。Value; Integer) 《 4 _ 

            Select c。Element(〃firstName〃)。ToString() & 〃 〃 & _ 

                   c。Element(〃lastName〃)。ToString() 



     Notice how the same LINQ syntax that you’ve seen in the previous examples is used; but  

the source of the data that is to be manipulated by LINQ is different。 Keep in mind that when  

you are manipulating data using LINQ; you are manipulating objects that may point to XML  

files; relational databases; or plain…vanilla data objects。 



The Important Stuff to Remember 



In this chapter; you learned about the basics of LINQ and how to write queries。 Here are the key  

points to remember: 



     o  LINQ is an API that sits on top of other technologies such as Visual Basic objects; rela

        tional databases; and XML documents。  



     o  LINQ can work effectively only if the underlying data source technology has been opti

        mized for LINQ。 Otherwise; you are left with having to load a single record set and then  

        manipulate that record set。 



     o  Regardless of the data source; the techniques used to query and write LINQ are identical。 



     o  When manipulating LINQ objects; the methods and properties associated with the  

        various data sources are different。 For example; when searching XML documents; you  

        can use XML Document Object Model (DOM) methods and properties that are not avail

        able when manipulating plain…vanilla objects。 



     o  LINQ is not just a syntax; but a series of extension methods associated with sets of data。  

        The methods allow for more sophisticated data pipelining and processing of  

        information。 


…………………………………………………………Page 438……………………………………………………………

416       CH AP T E R   1 5   ■    L E A R N I N G   A B OU T   L I N Q 



           Some Things for You to Do 



           The following are two exercises to help you apply what you’ve learned in this chapter。 



                1。  The solution for finding a frequency presented in this chapter went from text to text to  

                    calculate the statistics。 Can you think of another approach that would require minimal  

                    changes in the interface structure? Hint: the way the objects were parsed into objects  

                    borrowed code from another application。 Could that other application be used somehow?  



                2。  You saw a LINQ query embedding another LINQ query when finding the frequency of  

                    two numbers。 Rewrite the code to generate the frequency of all binations of single;  

                    pairs; and triples。 


…………………………………………………………Page 439……………………………………………………………

C  H  A  P  T  E  R     1  6 



■ ■ ■ 



Learning About Other Visual  

Basic Techniques 



This last chapter in the book is about tying up loose ends。 The techniques discussed in this  

chapter are those that you will use in specific situations。 This chapter covers the following  

topics: 



    o  How to use arithmetic operators to manipulate numbers 



    o  How to overload operators 



    o  When you might use the GoTo statement  



    o  How to use  generics constraints 



    o  How to use nullable types  



    o  How to use partial classes and methods  



Operators 



You have seen various operators used in examples throughout the book; such as the assignment  

operator (a = 3); and the logical operators ( If( a = b))。 Visual Basic has many more arithmetic  

operators that you can use to process types。 You can also define custom operators。  



Using Arithmetic Operators 



The subtraction ( …); multiplication (*); and division (/) operators are typically applicable to  

only numeric values。 These operators are directly parable to the mathematical operators  

you learned about in elementary school。 Let’s look at what the other arithmetic operators do。 



Addition 



The addition (+) operator is used to indicate the addition of two values; like this: 



a = c + 1 



     The addition has a left…hand side and right…hand side; separated by the equal sign (=)。  

On the right…hand side; the variable c is added to  1 and assigned to the variable a。 

                                                                                                          417 


…………………………………………………………Page 440……………………………………………………………

418       CH AP T E R   1 6   ■    L E A R N I N G   A B OU T   O TH E R   V IS U AL   B A SI C  TE C H N IQ U E S 



返回目录 上一页 下一页 回到顶部 2 2

你可能喜欢的