Anonymous types are useful only in the context of the method in which they are declared。
The variable points in the CountCustomers2() method is a collection of anonymous…type
objects that could be iterated as follows:
For Each customer in points
Console。WriteLine(〃Customer (〃 & _
customer。identifier & 〃)(〃 & _
customer。points & 〃)〃)
Next
The piler that translates the LINQ expression knows that the final result set contains
anonymous…type objects with the properties identifier and points。
Processing Multiple Streams
In all of the LINQ examples so far; a single result set has been manipulated; processed; and filtered。
You can process multiple inputs at the same time; but you will get a binatorial type answer。
For example; suppose you had this LINQ:
Dim set1 As Integer() = New Integer() {1; 2; 3; 4; 5}
Dim set2 As Integer() = New Integer() {1; 2; 3; 4; 5}
Dim set3 As Integer() = New Integer() {1; 2; 3; 4; 5}
Dim triples = _
From a In set1 _
From b In set2 _
From c In set3 _
Select New With {a; b; c}
In pseudo…code; the following would be identical。
…………………………………………………………Page 433……………………………………………………………
CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q 411
Dim items As List (Of Object) = New List(Of Object)()
For Each a In set1
For Each b In set2
For Each c In set3
items。Add(New With { a; b; c })
Next
Next
Next
When you specify multiple From clauses; you are creating a looping mechanism where
each item is iterated against the other elements。 This sounds useful; but it can have a disastrous
side effect: a seemingly innocent query can take much longer than it should。 After having
written the individual From statements; you can use Where and Select as usual。
Sorting the Results
After having selected elements; you will probably want to sort the result set。 Using LINQ; you
can sort by anything you deem important。 The obvious approach is to sort according to a number
or letter; but you could also sort according to length of the word。
Regardless of how you sort; in LINQ you use the keywords Order By or the method OrderBy()。
The following is a LINQ example that does an alphabetic sort。
Dim words As String() = { 〃cherry〃; 〃apple〃; 〃blueberry〃 }
Dim sortedWords = _
From w In words _
Order By w _
Select w
The phrase Order By is inserted before Select。 In this case; it will sort the words in ascending
alphabetical order。 The way that the Order By works is that the value of the variable is pared;
rather than the actual variable。If you want to sort in reverse order; you can use the keyword
Descending; as follows:
Dim sortedWords = _
From w In words _
Order By w Descending _
Select w
This approach allows you to perform sorts according to other values。 For example; you
could sort by the length of word; like this:
Dim sortedWords = _
From w In words _
Order By w。Length _
Select w
…………………………………………………………Page 434……………………………………………………………
412 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q
The Order By queries for the value of w。Length; which returns a number。 If that number
happens to be longer or shorter than another word; it is placed after or before the other word。
You could also sort according to multiple criteria。 For example; you could sort the words
alphabetically and then by length:
Dim sortedWords = _
From w In words _
Order By w; w。Length _
Select w
To sort by multiple criteria; append them to the Order By keywords; each separated by
a ma。
■Note The alphabetical and length sort is fruitless; because when you sort alphabetically; you will auto
matically sort by length。 The example is just for illustrative purposes。
Performing Set Operations
The last major topic that you need to know about when using LINQ is the ability to perform set
operations on a collection。 The major downside; however; to performing set operations is that
you must use the methods; and at the time of this writing; no LINQ mand syntax existed for
set operations。
Knowing about set operations is useful because they enable you to sort and organize multiple
result sets。 The examples in this section involve the Customer type again。 However; to make
sure that the set operations function properly; you need to implement the Equals() and
GetHashCode() methods; like this:
Friend Class Customer
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is Customer Then
Dim otherObj As Customer = TryCast(obj; Customer)
If otherObj。Identifier。pareTo(Identifier) = 0 Then
Return True
End If
End If
Return False
End Function
Public Overrides Function GetHashCode() As Integer
Return Identifier。GetHashCode()
End Function
Public Overrides Function ToString() As String
Return String。Concat(New Object() _
{〃Ide
小说推荐
- 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章