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……………………………………………………………
小说推荐
- 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章