Linq加入变量范围问题

时间:2014-01-22 10:30:49

标签: c# visual-studio-2010 linq join

我无法解决这个奇怪的问题。

我试图在linq语句中连接两个表,只检索表1中的记录在表2中没有相关行的记录。

之前我使用过Joins但由于某些原因我无法让VS识别linq语句中的第二个表。

EG。

var result = 
(from pc in _dataSource.Payments
join bc in _dataSource.BouncedCheques

on pc.PaymentID != bc.PaymentID //This is where the error occurs, VS does not recognise "bc"

where pc.CustomerNumber == getAccountNumber
& pc.IsDeleted == false
orderby pc.PaymentDate descending
select new PaymentAllocation
{
     PaymentId = pc.PaymentID,
     PaymentDate = pc.PaymentDate,
     CustomerNumber = pc.CustomerNumber,
     ChequeReference = pc.ChequeReference,
     PaymentValue = pc.PaymentValue,
     AllocatedValue = pc.AllocatedValue,
     UnallocatedValue = pc.PaymentValue - pc.AllocatedValue,
     ReceivedBy = pc.ReceivedBy,
     PaymentType = pc.PaymentType,
     PostedDate = pc.PostedDate
});

基本上问题是变量“bc”似乎没有得到识别,但我还有其他几个类似的Linq查询都能正常运行

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的问题是join的语法使用关键字equals而不是标准布尔运算符。

尝试用表格的笛卡尔积替换你的联接:

from pc in _dataSource.Payments
from bc in _dataSource.BouncedCheques
where
pc.PaymentID != bc.PaymentID
&& pc.CustomerNumber == getAccountNumber
& pc.IsDeleted == false

答案 1 :(得分:0)

在join子句中,您应该使用equals关键字:

尝试:

on pc.PaymentID equals bc.PaymentID