我很难弄清楚如何进行左连接并拉出连接为空的记录,或者连接工作,但是支付的列是0.这是sql I' m试图重新创建
SELECT *
FROM JobsCompleted
LEFT JOIN SqWar ON JobsCompleted.JobFk = SqWar.JobFk
WHERE SqWar.SqWarId IS NULL OR SqWar.Paid = 0
这是我迄今为止在linq中所拥有的。
'not sure here how I do if the join is null, or joins, but paid is 0
Dim jobIds = (From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into SqGroup = Group
Where jc.DateCompleted >= fromInput.SelectedDate And jc.DateCompleted <= toInput.SelectedDate And
SqGroup Is Nothing) 'not sure here how I do if it's null or paid = 0
答案 0 :(得分:0)
使用Group Join
,如果你想要一个SQL LEFT JOIN
,特别是因为你想要从右侧测试一个字段,你需要使用第二个From
子句DefaultIfEmpty
:
Dim jobIds = From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into Group
From sqw In Group.DefaultIfEmpty()
Where sqw Is Nothing Or sqw.Paid = 0
Select New With { jc, sqw }
请注意,LINQ没有直接等效于SQL SELECT *
所以我返回了一个带有子对象的匿名对象。如果您希望将结果展平,则必须明确列出所有字段。
如果您想处理加入组,您还可以按标准过滤每个组:
Dim jobIdsG = From jc In db.JobsCompleteds
Group Join sqw In db.SqWars On sqw.JobFk Equals jc.JobFk Into Group
Select New With {jc, .sqGroup = Group.Where(Function(sq) sq.Paid = 0)}