【问题标题】:Left outer join in Linq to Entities (asp.net vb)Linq to Entities 中的左外连接 (asp.net vb)
【发布时间】:2015-08-06 12:33:08
【问题描述】:

我在这个问题上被困太久了,我认为这只是我的经验不足。我已经查看了许多类似的文章,但是当我将其应用于我的情况时似乎没有任何工作。

我在 EF6 中使用 linq 到实体。我有两个简单的表“LoanRepayment”和“LoanReceipt”。它们与从 LoanRepayment.LoanPaymentId 到 LoanReceipt.LoanPaymentId 的外国相关联。有 48 个固定贷款支付,其中 4 个有收据。我只想创建一个包含 48 行的表,其中包含 LoanRepayment 表中的所有 48 行以及存在的任何收据数据(即 4)。尚未支付的贷款旁边的其余单元格应为空白。

Dim queryLoanRepayments = (From lp In proxifundContext.LoanRepayments Group 
Join lr In proxifundContext.LoanReceipts On lr.LoanPaymentId Equals
lp.LoanPaymentId Into paymentlist = Group From lr In 
paymentlist.DefaultIfEmpty() Select lp.InstallmentNo, lp.InstallmentAmount, 
lp.Penalty, lp.PaymentDate, If(lr.ReceiptDate Is Nothing, String.Empty, 
lr.ReceiptDate), If(lr.Amount Is Nothing, String.Empty, lr.Amount)).ToList()

当我运行上面的查询时,intellisense 会在两条 If 语句中加下划线,并带有以下消息:“范围变量名称只能从不带参数的简单名称或限定名称中推断出来”。

我做错了什么?

【问题讨论】:

    标签: vb.net linq-to-entities left-join


    【解决方案1】:

    您应该自己为计算的属性命名,因为 VB 无法推断它们(就像从简单属性中推断出来的那样):

    ...
    Select lp.InstallmentNo, 
           lp.InstallmentAmount, 
           lp.Penalty,
           lp.PaymentDate,
           ReceiptDate = If(lr.ReceiptDate Is Nothing, String.Empty, 
           lr.ReceiptDate),
           Amount = If(lr.Amount Is Nothing, String.Empty, lr.Amount)
    

    【讨论】:

      【解决方案2】:

      感谢格特·阿诺德。你为我指明了正确的方向。实际有效的语句是:

      Dim queryLoanRepayments = (From lp In proxifundContext.LoanRepayments Group 
      Join lr In proxifundContext.LoanReceipts On lr.LoanPaymentId Equals 
      lp.LoanPaymentId Into paymentlist = Group From lr In 
      paymentlist.DefaultIfEmpty() 
      Select lp.LoanApplicationId, 
             lp.InstallmentNo, 
             lp.InstallmentAmount, 
             lp.Penalty, 
             lp.PaymentDate, 
             ReceiptDate = If(lr.ReceiptDate = Nothing, String.Empty, CStr(lr.ReceiptDate)), 
             Amount = If(lr.Amount = Nothing, String.Empty, CStr(lr.Amount))).ToList()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        相关资源
        最近更新 更多