linq left join 測試代碼 有數據EXCEL表 謝謝大神幫忙解決下:
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
------解决方案--------------------
给你个例子,你参考一下,我想你可以写的出来:
Public Sub Linq105()
Dim categories() = {"Beverages", "Condiments", "Vegetables", "Dairy Products", "Seafood"}
Dim productList = GetProductList()
Dim query = From c In categories _
Group Join p In productList On c Equals p.Category Into Group _
From p In Group.DefaultIfEmpty() _
Select Category = c, ProductName = If(p Is Nothing, "(No products)", p.ProductName)