梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
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