日期:2014-05-20  浏览次数:20873 次

VB.NET linq 如何left join (帶測試代碼)
本帖最后由 q107770540 于 2014-01-18 19:54:48 编辑
linq left join 測試代碼 有數據EXCEL表 謝謝大神幫忙解決下:
http://t-1.tuzhan.com/79de13608b2d/p-2/f1927409c2a43ebf/2014/01/18/17/8e2648419ca744069b581e83d6646ce0.jpg

2個datatable結果集合併(LEFT JOIN)
excel2003測試表
SUVZHG:
http://pan.baidu.com/s/1i39Nkln
TOPRC:
http://pan.baidu.com/s/1gd417Xp

  
Function SUVZHG() As DataTable
        Dim fileName As String
        Dim kk As New OpenFileDialog()
        kk.FileName = "SUVZHG"
        kk.Filter = "Excle恅璃(*.xls)|*.xls"
        If kk.ShowDialog() = DialogResult.OK Then
            fileName = kk.FileName
            Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
            Dim dr As New DataTable
            Dim da As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
            Try
                da.Fill(dr)
                SUVZHG = dr
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        End If
    End Function
    Function TOPRC() As DataTable
        Dim fileName As String
        Dim kk As New OpenFileDialog()
        kk.FileName = "TOPRC"
        kk.Filter = "Excle恅璃(*.xls)|*.xls"
        If kk.ShowDialog() = DialogResult.OK Then
            fileName = kk.FileName
            Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
            Dim dr As New DataTable
            Dim da As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
            Try
                da.Fill(dr)
                TOPRC = dr
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        End If
    End Function
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim q = From emp In TOPRC() Group Join emc In SUVZHG() On emp("JSX") Equals emc("JSX") Into DataGroup = Group
       From row In DataGroup.DefaultIfEmpty()
Select emp, Order = row
        DG1.DataSource = q.CopyToDataTable
    End Sub
 

------解决方案--------------------