Write a console application that populates the draws table using a lottery file。 Write a
console application that dumps the contents of the draws table as a lottery file。
2。 Create a console application that accepts as mand…line arguments a winner and
the date of the draw。 Your console application must account for doubles and populate
both the persons and winners tables。
…………………………………………………………Page 416……………………………………………………………
…………………………………………………………Page 417……………………………………………………………
C H A P T E R 1 5
■ ■ ■
Learning About LINQ
In the previous chapter; you learned about how to access a database using the traditional
ADO technologies。 When you make ADO requests; you are making SQL calls and
organizing the result set data using SQL。
Language Integrated Query (LINQ) is a technology that lets you organize your results in a
consistent manner; regardless of the underlying source of the data。 Why yet another technology
to query information? The answer is related to XML。
XML is a technology used to represent information in a hierarchical manner。 You saw an
example of XML in Chapter 12。 XML has solved many problems in an elegant and understand
able manner。 One of the solutions proposed by XML is the ability to reference information in
an XML structure with XPath; which is a way of referencing an object structure using a set of
filters。 Using XPath; you can find any node; and the filters can include the presence of depen
dent elements; which is not easily possible in other technologies。 Put simply; XPath and XML
are very powerful techniques used to find information。
You can consider XML and XPath as inspiration for LINQ。 Where LINQ and XML XPath
deviate is that LINQ can be used to query Visual Basic collection objects; XML documents; and
relational databases。 Think of LINQ as a general mechanism used to search a collection of
information。 The focus of this chapter will be to explain the mechanics of LINQ; and demon
strate how to write queries and use the methods associated with the LINQ library。
Finding the Frequency of Winning Numbers
The lottery application we’ve been working with in previous chapters collects information
about lottery drawings to predict the next set of winning lottery numbers。 The idea is to find
patterns。 Again; the reality is that lottery drawings are random; so even if you could identify
patterns; that wouldn’t mean you could predict winning numbers。 However; what is interesting
about this problem is that you can use LINQ to slice and dice the data。 LINQ lets you keep the
data as objects; and to perform SQLand XPath…like operations; as you’ll see in this chapter。
In Chapter 10; you saw a streaming architecture; where a console application read in text
data and spat out text data。 You also saw examples of text…to…binary and binary…to…text conver
sions。 The application in this chapter (in the FrequencyProcessor project) will read in text; process
the data; and then generate text describing the frequency of lottery numbers。 Thus; we need to
use the text…to…text interface; which was defined in Chapter 10 as follows:
395
…………………………………………………………Page 418……………………………………………………………
396 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q
Public Interface IProcessor
Function Process(ByVal input As String) As String
End Interface
The input is a text stream that looks like this:
2006。03。11 3 7 15 28 30 38 44
2006。03。15 10 18 30 34 41 43 5
2006。03。18 3 11 12 16 20 40 9
2006。03。22 2 3 7 13 42 43 41
2006。03。25 3 10 36 40 43 44 35
2006。03。29 3 4 8 16 34 39 45
If you want to find the frequency of the individual numbers; you could parse each individual
number; and then increment the count of the individual number as an array; like this:
Dim frequency As Integer = 0
Dim splitUpText() As String = lineOfText。Split(New Char() {〃 〃c})
frequency(Integer。Parse(splitUpText(0))) += 1
frequency(Integer。Parse(splitUpText(1))) += 1
。 。 。
The split line of text contains the list of numbers; separated by spaces; which is then converted
to a series of numbers; each of which is used as an index for the frequency。 The solution is fast
and works; but it has a big problem: it is not extendable。 The solution solves a single problem
and only a single problem。
For example; let’s say that you want to figure out other statistical information; such as which
numeric binations occur most often。 Using the previous solution; that would require
reparsing the text stream again。 That is an expensive and tedious solution。 The better solution
would be to convert the stream into a series of objects that could be processed。
However; the problem at hand is calculating the frequency of individual numbers。 So why
exert the extra effort if the solution that solves the single problem works? Writing good code
means solving problems using a generic but specific approach。 You want to write specific code
so that you do not get bogged down in thinking about details in the future; and you want to
write generic code so that any future requirement does not cause you to pletely rewrite the
old application。 Knowing when to write specific code and when to write generic code is really
a matter of experience—the only way to learn is to write code。
Extending the Lottery…Prediction System
In Chapter 10; the IPr
小说推荐
- 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章