LINQ 於 VB.NET

有一些讀者問起,[極意之道-.NET Framework 3.5 資料庫開發聖典]是否會有VB.NET版本,

這點要視出版社而定,一般來說,如果該書一刷尚未賣

   有一些讀者問起,[極意之道-.NET Framework 3.5 資料庫開發聖典]是否會有VB.NET版本,

這點要視出版社而定,一般來說,如果該書一刷尚未賣完,出版社不太可能會投入資金來出版

同一主題,但不同語言的書,所以會不會有VB.NET 版本,得看銷量而定.

anywhere,我其實一直都有個計畫,就是將此書中的範例改寫為VB.NET版本,如果VB.NET版能

順利出版的話,那範例可以放在該書中.

如果出版社在一刷賣完後,仍不願讓我出版VB.NET版,那麼屆時我可以將範例放在BLOG中,供

買了此書之VB.NET讀者下載.

其實,就LINQ語法上來說,VB.NET與C#差異並不大,請見以下程式即可明瞭

 

Imports System.Linq
 
Public Class Program
 
    Shared Sub Main()
        TestJoin()
    End Sub
 
    'let 運算子
    Shared Sub UseLet()
        Dim list As String() = {"Code6421 Huang", "Tom Do", "Cathy Chang"}
        Dim result = From s1 In list _
                     Let words = s1.Split(" ") _
                     From word In words _
                     Let w = word.ToLower() _
                     Where w(0) = "c" _
                     Select word
        For Each item In result
            Console.WriteLine(item)
        Next
        Console.ReadLine()
    End Sub
 
 
    'lambda expression for vb.net
    Shared Sub TestLastWithCondition()
        Dim numbers() = {8, 9, 10, 7}
        Console.WriteLine(numbers.Last(Function(x) x > 7))
        Console.ReadLine()
 
    End Sub
 
    '匿名型別
    'join運算式
    Shared Sub TestJoin()
        Dim p1() = {New With {.Name = "code6421", .Address = "Taipai"}, _
                    New With {.Name = "tom", .Address = "Taipai"}, _
                    New With {.Name = "jeffray", .Address = "NY"}}
 
        Dim p2() = {New With {.Name = "code6421", .Title = "Manager"}, _
                    New With {.Name = "tom", .Title = "Director"}, _
                    New With {.Name = "jeffray", .Title = "Programmer"}}
 
        Dim p3() = {New With {.Name = "code6421", .Hand = "Right"}, _
                    New With {.Name = "tom", .Hand = "Right"}, _
                    New With {.Name = "jeffray", .Hand = "Left"}}
        Dim p4 = From s In p1 _
                 Join s1 In p2 On s.Name Equals s1.Name _
                 Join s2 In p3 On s.Name Equals s2.Name _
                 Select New With {.Name = s.Name, .Address = s.Address, .Title = s1.Title, .Hand = s2.Hand}
        For Each item In p4
            Console.WriteLine("Name {0}, Address {1}, Title {2}, Hand {3}", item.Name, item.Address, item.Title, item.Hand)
        Next
        Console.ReadLine()
 
    End Sub
 
 
End Class
 

 

差別較大的是,TestLastWithCondition程序中的lambda寫法,及TestJoin中的Select new(匿名型別)之寫法.