【发布时间】:2015-05-07 16:02:09
【问题描述】:
我正在尝试在 linq 中实现与实体 (VB.NET) 的连接。 sql 看起来像这样:
SELECT someField FROM SomeTable a
LEFT JOIN someOtherTable b ON a.Key = b.Key AND b.Name <> ''
这只是查询的一小部分,还有很多其他的连接,但问题是连接上的 where 子句。我通常可以通过加入匿名类型来做到这一点,但我不确定如何使用匿名类型方法在加入中实现 b.Name '' 。例如:
Return From document In dbContext.Documents
Group Join assessorNumberRow In dbContext.AssessorNumbers
On New With {document.DocumentId, .IsEmpty = False} Equals {assessorNumberRow.DocumentId, assessorNumberRow.AssessorNumber1}
Into foundAssessorNumbers = Group
From foundAssessorNumber In foundAssessorNumbers.DefaultIfEmpty()
但这行不通,因为无法比较匿名类型,因为我不太确定如何设置条件以确保在联接中不为空 - 不在最后的 where 子句中。
我想我可以在决赛中做这样的事情:
Where foundAssessorNumber Is Nothing OrElse foundAssessorNumber.Number <> string.empty.
但我认为在实际连接中包含 where 条件的语法更容易遵循,而不是将其附加到整个查询的末尾。
有什么想法吗?
【问题讨论】: