vb的LINQ实现

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
 
 
 

转载于:https://www.cnblogs.com/RealAlex/p/3175361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值