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
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 其他
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 其他
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 其他
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 其他
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 其他
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 其他
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 文学名著
- 最新章:第230章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 其他
- 最新章:第27章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 其他
- 最新章:第28章