我很难弄清楚如何将多列上的简单 SQL LEFT OUTER JOIN 和 where 子句转换为有效的 Linq-to-Entities 查询。只有两张桌子。我需要 Table1 中所有行的值,无论 Table2 中的匹配如何,但事实证明连接多个列很困难。我还需要在查询中做一个简单的计算,但也找不到。SQL 中的查询如下所示:
select t1.tableid1,
t1.tableid2,
t1.fieldvalue1,
t2.fieldvalue2,
isnull(t2.fieldvalue2,0) / t1.fieldvalue1 as calcvalue
t1.fieldvalue1 - isnull(t2.fieldvalue2,0) as calcvalue2
from table1 t1
left outer join table2 t2
on t1.tableid1 = t2.tableid1
and t1.tableid2 = t2.tableid2
and t1.tableid3 = t2.tableid3
where t1.tableid1 = @somevalue
我可以进行单列左连接,但我似乎找不到多列的正确语法,更不用说不知道如何添加或减去列上的值。下面是我最好的猜测,但我得到了一个 Type Expected 错误(在“new”之后和第一个左括号之前):
On New (t2.Field(Of String)("tableid1"), t2.Field(Of String)
最佳的揣测:
Dim query = From t1 In dtTable1 _
Group Join t2 In dtTable2 _
On New (t2.Field(Of String)("tableid1"), t2.Field(Of String)("tableid2")) Equals _
(t1.Field(Of String)("tableid1"), t1.Field(Of String)("tableid2")) _
Into t2outer = Group _
From t2 In t2outer.DefaultIfEmpty() _
Select New With _
{ _
.t1_tableid1 = t1.Field(Of String)("tableid1"), _
.t1_tableid2 = t1.Field(Of String)("tableid2"), _
.t1_fieldvalue1 = t1.Field(Of Integer)("fieldvalue1"))
.t2_fieldvalue2 = If(t2 Is Nothing, CType("0", Integer), t2.Field(Of Integer)("fieldvalue2"))
}
@Gert这让我足够接近。我认为格式略有偏差,因为我必须添加大括号而不是括号,并且它在“Key”关键字和 id 之间放置了一个空格:
On New With {
Key .id1 = t2.Field(Of String)("tableid1"), _
Key .id2 = t2.Field(Of String)("tableid2")
} _
Equals _
New With {
Key .id1 = t1.Field(Of String)("tableid1"), _
Key .id2 = t1.Field(Of String)("tableid2")
} _
感谢 Gert,我能够按照 SQL 查询中的描述进行这项工作。请参阅下面的 SQL 查询的完整工作表示形式:
Dim query = From t1 In dtTable1 _
Where (T1.Field(Of String)("fieldvalue1") = SOMEVALUE) _
Group Join t2 In dtTable2 _
On New (t2.Field(Of String)("tableid1"), t2.Field(Of String)("tableid2")) Equals _
(t1.Field(Of String)("tableid1"), t1.Field(Of String)("tableid2")) _
Into t2outer = Group _
From t2 In t2outer.DefaultIfEmpty() _
Select New With _
{ _
.t1_tableid1 = t1.Field(Of String)("tableid1"), _
.t1_tableid2 = t1.Field(Of String)("tableid2"), _
.t1_fieldvalue1 = t1.Field(Of Integer)("fieldvalue1"))
.t2_fieldvalue2 = If(t2 Is Nothing, CType("0", Integer), t2.Field(Of Integer)("fieldvalue2"))
.calcvalue = If(t2.Field(Of Integer)("fieldvalue2") Is Nothing, CType("0", Integer), t2.Field(Of Integer)("fieldvalue2")) _
/ CType(t1.Field(Of String)("fieldvalue1"), Integer),
.calcvalue2 = CType(t1.Field(Of String)("fieldvalue1"), Integer) _
- If(t2.Field(Of Integer)("fieldvalue2") Is Nothing, CType("0", Integer), t2.Field(Of Integer)("fieldvalue2")) _
}