ate the
results and return a True or False。
Any() Like All(); except that the question is changed to test if any of the objects meet
the criteria; such as having a value greater than 10。 If so; then a True value is
returned; otherwise; a False value is returned。
Average() Calculates the average of a sequence of values。 The average value returned is
a numeric Decimal value。 This method is a bit odd; because to calculate an
average; you need numbers; even though the lambda expression could calcu
late the average of objects。
…………………………………………………………Page 429……………………………………………………………
CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q 407
Table 15…1。 Some Methods for Filtering and Manipulating Lists (Continued)
Method Description
Cast() Returns a list where each item is converted from the list type to another type。
This is a good method to use when you need to perform bulk conversions of
instance types in a list。
Concat() Concatenates two lists。
Contains() Verifies whether an item is present in the list。 The method uses a lambda
expression to determine whether the specified item is in the list; and returns
True or False accordingly。
ConvertAll() Returns a list where each item is converted from the list type to another type。
This is a good method to use when you need to perform bulk conversions of
instance types in a list。
Distinct() Removes all duplicates from a list。 By default; the implementation of Distinct()
checks for equality by calling GetHashCode() first; and then calling Equals() if
necessary。 A variation of the Distinct() method is to supply an IEqualityparer
interface instance that can be used to determine whether two types are equal。
However; a better approach would be to implement GetHashCode() and
Equals()。
Except() Takes the current list and a passed…in list and performs a difference between
the two sets; which is returned to the caller as a new dataset。 The equality tests
are identical to Distinct()。
Find() Finds an element of a particular list。 Note that the lambda expression you use
when it has found an element will cause the Find() method to stop processing
the list and return what you marked as found。
FindAll() Like Find(); except you can find multiple elements in a list。 This is like the
Where() method。
FindLast() Like Find(); except the search starts at the end of the list。
ForEach() An iterator that uses a lambda expression to process each element。 The
ForEach() method is a simplification of the code illustrated in Chapter 9。
GroupBy() Takes a list and splits it into specific groupings as per the provided lambda
expression。 For example; you could use it to split the earnings of individuals
into brackets。
Intersect() Takes the current list and a provided list and determines the elements that are
mon to both lists。 Uses the same equality tests as Distinct()。
Max() Finds the maximum value of a list。
Min() Finds the minimum value of a list。
Reverse() Reverses the order of the list。
Select() Selects from the iteration being executed。
SelectMany() Selects many items from a list where the selected items form another list。
Sum() Calculates the sum of a list。
Union() Takes the list and the passed…in list and calculates the union of the two lists。 Uses
the equality test as defined in the Distinct() method。
…………………………………………………………Page 430……………………………………………………………
408 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q
■Note With Visual Basic 2008; lists and the manipulation of lists have dramatically changed for the better。
The general structure is to define methods that allow a developer to specify a lambda expression that is then
chained together with other methods。 Take some time to learn about all of the possibilities。
As an example of the power of the various methods; consider the following code; which
pacts the frequency code into a couple of lines of source code。
Function FrequencyOfANumberFunc(ByVal numberToSearch As Integer) As Integer
Return _tickets。SelectMany( _
Function(ticket) ticket。Numbers)。Where( _
Function(num) num = numberToSearch)。Count()
End Function
Here; each ticket is iterated by calling the SelectMany() method。 This returns an array of
numbers; which represents the drawn numbers。 The purpose of SelectMany() is to bine
the individual arrays of numbers into a large array of numbers。 The code then calls Where()
to filter out only those numbers that equal the number to search for; and finally the Count()
method is called to return the number of found values。
The following sections present examples of using the extension methods wi
小说推荐
- 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章