method is called to return the number of found values。
The following sections present examples of using the extension methods with LINQ。 They
are in the LINQExamples project; which is a console application。
■Note In all of the examples; I have taken shortcuts for simplicity。 So you will see some coding practices
that are not remended; such as creating public data members and not using properties。
Selecting and Altering Data
When running a LINQ query; the data that you filter and manipulate does not need to stay in
its original form。 Let’s say that you have a list of customers; and you have identified a set of
customers who deserve more loyalty points。 You want to select those customers; increment
their points; and then return the list of altered customers。 To do that; you can mix LINQ with
the extension methods。
Consider the following simplified customer declaration。
Class Customer
Public Identifier As String
Public Points As Integer
Public Overrides Function ToString() As String
Return 〃Identifier (〃 & Identifier & 〃) Points (〃 & Points & 〃)〃
End Function
End Class
A list will be created with two customers; where one customer has no points and the other
one does。 Here is the source code to create that list:
…………………………………………………………Page 431……………………………………………………………
CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q 409
Private Function CreateList() As Customer()
Return New Customer() { _
New Customer() With {。Identifier = 〃Person 1〃; 。Points = 0}; _
New Customer() With {。Identifier = 〃Person 2〃; 。Points = 10}}
End Function
The customers that have enough points are selected and rewarded with extra points。 To do
that; use the following LINQ statement。
Function Increment(ByVal pCustomer As Customer; ByVal index As Integer) _
As Customer
pCustomer。Points += 5
Return pCustomer
End Function
Sub CountCustomers()
Dim points = (From customer In CreateList() _
Where customer。Points 》 5 _
Select customer)。Select(AddressOf Increment)
Console。WriteLine(〃Count is (〃 & points。Count() & 〃)〃)
End Sub
The LINQ query is bined with a modification operation。 The LINQ statement that uses
From; Where; and Select is not new。 New are the parentheses enclosing the LINQ statement。 By
using a set of parentheses; you are identifying the LINQ statement as an object that references
a result set。
In the example; the method called on the LINQ statement is Select()。 Using the Select()
method; each item in the result set is iterated and passed as a parameter (pCustomer) to the
lambda expression (Increment())。 Passed with the item is the index of the item in the list。 The
role of the lambda expression is to do something with the item and return what should be used as
a basis for another list。 In the example; an instance of the type Customer is passed in to Increment();
and an instance of type Customer is passed out。 But before the instance is returned; it is manip
ulated to get an additional five bonus points。
What might concern you is that there is no test to check if the customer actually warrants
earning five bonus points。 That would be a concern if you were not using the LINQ expression。
The LINQ expression is responsible for filtering out only those customers who should get the
additional bonus points。 Thus; when the method Select() is called; you are 100% sure that
only the customers who should get bonus points actually get bonus points。 Think of this as
building a pipeline of manipulations。
Selecting with Anonymous Types
The Select() method and statement are used to generate a result set after finding a particular
element。 As demonstrated in the previous section; a Select statement is used to generate a new
result set。 For example; what if you want to find all of the customers who fulfill a certain crite
rion; but do not want to copy all of the associated data? You might want only the customer
identifier and accumulated points。 To do that; you could modify the Select() part of the LINQ
statement to return a new type that you declare dynamically。 The following is the previous
example rewritten to use an anonymous type。
…………………………………………………………Page 432……………………………………………………………
410 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q
Function Increment2(ByVal pCustomer As Customer; ByVal index As Integer)
pCustomer。Points += 5
Return New With {。identifier = pCustomer。Identifier; _
。points = pCustomer。Points}
End Function
Sub CountCustomers2()
Dim points = (From customer In CreateList() _
Where customer。Points 》 5 _
Select customer)。Select(AddressOf Increment2)
Console。WriteLine(〃Count is (〃 & points。Count() & 〃)〃)
End Sub
In the example; the Return statement in the Increment2() method uses the keyword New
without a type identifier; but with the syntax of an object initializer。 This is defining an anony
mous type。 An anonymous type is an object instance that has no explicitly named type。 The
anonymous type has properties; but it does not have methods。
Anonymous types are useful only in the context of the method in which they are declared。
The var
小说推荐
- 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章