Linq to Entities中的左外连接(asp.net vb)

时间:2015-08-06 12:33:08

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

我长期以来一直坚持这个问题,而且我认为这只是我的经验不足之处。我已经查看了很多类似的文章,但是当我将它应用到我的情况时,似乎都没有。

我将linq用于EF6的实体。我有两个简单的表' LoanRepayment'和' LoanReceipt'。它们与外国人从LoanRepayment.LoanPaymentId链接到LoanReceipt.LoanPaymentId。有48个固定的LoanPayments,其中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语句:只能从没有参数"的简单或限定名称推断出Range变量名。

我做错了什么?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

谢谢Gert Arnold。你指出我正确的方向。实际的声明是:

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()