友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

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

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




                  Return String。Concat(New Object() _  

                    {〃Identifier (〃; Me。Identifier; 〃) Points (〃; Me。Points; 〃)〃}) 

              End Function 


…………………………………………………………Page 435……………………………………………………………

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



    Public Identifier As String 

    Public Points As Integer 

End Class 



■Note  The GetHashCode() implementation here is rudimentary。 In the source code that es with this  

book; you will find a GetHashCode library class; which makes it simpler to implement GetHashCode()。 The  

source code is in the project ServerSideSpreadsheet/Devspace。Trader。mon/Automators。 



     Look at how GetHashCode() and  Equals() are implemented。 Notice that the  points data  

member is ignored。 In the case of a customer; this is acceptable; because a customer with iden

tical identifiers but unequal points does not imply two separate customers。  

     Implementing Equals() and GetHashCode() for custom types is absolutely imperative; because  

the set operations use that information to determine whether two objects are identical。 If you  

don’t implement either method; the set operations will use the default implementations of  

Equals() and GetHashCode(); which are inplete and will give you the wrong results。 

     The next step is to create two separate lists of customers。 In this example; both lists contain the  

same valued customer。 Realize that the identical customer is not the same object instance; but  

contains the same values。 



        Dim customers1 As Customer() = New Customer() { _ 

            New Customer() With {。Identifier = 〃Person 1〃; 。Points = 0}; _ 

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

        Dim customers2 As Customer() = New Customer() { _ 

            New Customer() With {。Identifier = 〃Person 3〃; 。Points = 0}; _ 

            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 t
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!