2014-01-22 67 views
0

真的很奇怪的問題,我無法解決。Linq加入變量範圍問題

我試圖連接兩個表的LINQ語句只檢索記錄,其中在表1中的記錄在表格中沒有相關行我已經使用2

之前加入,但由於某種原因,我不能讓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查詢,所有的工作做好

任何想法?

回答

1

您的問題是,連接的語法使用關鍵字equals而不是標準的布爾運算符。

嘗試更換你通過你的表的笛卡爾積連接:

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

這是否意味着除非我正在做一個標準的等於(或覆蓋等於),我必須這樣做嗎?這似乎是進行不同連接的非常低效的方式。 –

+0

我認爲這個想法是,你不使用equals運算符的連接沒有什麼比具有過濾器的笛卡爾產品更能提供性能。 –

0

在連接語句,你應該使用等於關鍵字:

嘗試:

on pc.PaymentID equals bc.PaymentID