你能帮助我理解linq,我几乎整天都在谷歌搜索它,但仍然无法正确理解架构思维。
这是问题所在,
我有来自不同数据库的2个数据表,我希望在datagridview
中显示左连接dataset
a和b在3列(OrderNum,OrderLine,OrderRelNum
)中
在sql中,查询看起来像这样
Select *
From dataset1 a
left join dataset2 b
on a.OrderNum = b.OrderNum
and a.OrderLine = b.OrderLine
and a.OrderRelNum = b.OrderRelNum
问题是如何在Linq中执行此查询并在datagridview中显示它们?
这是我的解决方案,用于获取正确的查询并将数据源数据到datagridview
Public Sub DataSetLinq120()
strSQL = "SELECT * FROM po "
strSQL2 = "SELECT * FROM PO"
Dim DA As New OdbcDataAdapter(strSQL, Connection)
Dim DS As New DataSet
DA.Fill(DS, "Source1")
Dim DA2 As New OdbcDataAdapter(strSQL2, Connection2)
Dim DS2 As New DataSet
DA2.Fill(DS2, "Source2")
Dim dt = DS.Tables("Source1")
Dim dt2 = DS2.Tables("Source2")
Dim custQuery = (From a In dt _
Group Join b In dt2 On a!OrderNum Equals b!OrderNum _
And a!OrderLine Equals b!OrderLine _
And a!OrderRelNum Equals b!OrderRelNum _
Into Data1 = Group _
From c In Data1.DefaultIfEmpty
Select New With {
.OrderNumber = a!OrderNum,
.OrderLine = a!OrderLine,
.OrderRelNumber = a!OrderRelNum,
.Stock_Status = If(c Is Nothing, "", c.Field(Of String)("Stock_Status")),
.Comments = If(c Is Nothing, "", c.Field(Of String)("Comments"))}).ToList
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = custQuery
End Sub
答案 0 :(得分:0)
以下是来自MSDN的关于如何在LINQ中执行左外连接的好文章:https://msdn.microsoft.com/en-us/library/bb397895.aspx
您应该可以轻松地创建LEFT JOIN。
除此之外,我不确定为什么你不想在这个例子中简单地用SQL来处理左连接。如果您不需要原始数据集,我不明白为什么您不能直接在SQL中执行此操作。