【问题标题】:Linq Group Join with Where clauseLinq Group Join with Where 子句
【发布时间】: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 条件的语法更容易遵循,而不是将其附加到整个查询的末尾。

有什么想法吗?

【问题讨论】:

    标签: vb.net linq


    【解决方案1】:

    你之前也可以这样做,例如向左连接实体添加一个Where 扩展方法

    (不习惯vb.net linq语法,所以可能有错误)

    Return From document In dbContext.Documents
                Group Join assessorNumberRow In dbContext.AssessorNumbers.Where(Function(a) a.Number <> string.Empty))
                On ...
    

    【讨论】:

    • 伙计,我不敢相信我没想过那样做。非常好,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多