vb实现LINQ非常简单的例子:
Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} Dim allNumbers = From number In numbers For Each num In allNumbers Console.WriteLine(num & " ") Next
LINQ中Let用法:
let关键字,可以让我们的linq查询不单单仅返回IEnumberable<T>类型的结果了,一个linq语句可以添加多个let关键字。
Public Sub TestLet() Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} 'let关键字,允许我们定义第二个变量,并存储特定值。本例中,存储了,是否偶数的判断 '这意味着,返回值可以不单单只是一个IEnumerable(of T)类型的值了。 Dim myEvens = From number In numbers Let isEven = (number Mod 2 = 0) For Each even In myEvens Console.WriteLine(even.number & " " & even.isEven) Next End Sub
LINQ中select用法:
Dim customerInfo = From cust In Customers, ord In cust.Orders _ Select myDate = ord.OrderDate, _ name = cust.FirstName & cust.LastName For Each info In customerInfo Console.WriteLine(info.myDate & info.name) Next
LINQ中的查询结果
LINQ的延迟查询
Dim customerInfo = From cust In Customers, ord In cust.Orders _ Select myDate = ord.OrderDate, _ name = cust.FirstName & cust.LastName Dim count = customerInfo.Count
延迟执行demo:
Dim seattleCustomers = From cust In Customers Where cust.City = "Seattle" Dim recentOrders = From ord In orders Where ord.OrderDate.Year = 2012 Dim cusInfos = From cust In seattleCustomers, ord In recentOrders Where cust.CustomerId = ord.CustomerId Select name = cust.LastName & ", " & cust.FirstName, _ cust.CustomerId, ord.OrderDate Order By OrderDate, name '上面LINQ只有在此处运行时,才被执行 For Each custInfo In cusInfos 'take action on the data here Next
参考文章:
http://msdn.microsoft.com/zh-cn/magazine/cc163401.aspx