2015-01-08 12 views
5

Tôi có truy vấn này:LINQ nhiều điều kiện tham gia sử dụng các biến thêm

SELECT * 
FROM transaction t 
JOIN transactionDetail toTrans ON t.id = toTrans.tId and toTrans.FlowDirection= 1 
JOIN transactionDetail fromTrans ON t.id = fromTrans.tId and fromTrans.FlowDirection= 0 

Mà tôi đã cố gắng để tái sử dụng các loại vô danh, như được giải thích here.

byte toFlow = 1; 
byte fromFlow = 1; 

from trans in data.Transactions 
join toTrans in data.TransactionDetails on new {trans.id, toFlow} equals new {toTrans.tId, toTrans.FlowDirection} 
join fromTrans in data.TransactionDetails on new { trans.id, fromFlow } equals new { fromTrans.tId, fromTrans.FlowDirection } 

Flowdirection luôn là 1 hoặc 0, vì vậy tôi đang sử dụng byte toFlow. Tuy nhiên, điều này đưa ra lỗi:

The type of one of the expressions in the join clause is incorrect.

Theo câu trả lời this, cả tên và loại cần phải khớp. Điều này có nghĩa là:

byte FlowDirection= 1; 

from trans in data.Transactions 
join toTrans in data.TransactionDetails on new {trans.id, FlowDirection} equals new {toTrans.tId, toTrans.FlowDirection} 
join fromTrans in data.TransactionDetails on new { trans.id, FlowDirection} equals new { fromTrans.tId, fromTrans.FlowDirection } 

Hoạt động nào! Tuy nhiên, tham gia thứ hai cần phải có một FlowDirection giá trị 0 thay vì 1. Làm thế nào tôi có thể thay đổi giá trị của FlowDirection? Tôi không thể thay đổi tên của biến hoặc trừ 1 bên trong đối tượng ẩn danh, nếu không thì điều này sẽ dễ dàng.

+3

Tại sao bạn không thể chỉ sử dụng hai hằng số hoặc chỉ truyền các chữ cái '(byte) 0' và' (byte) 1'? – StuartLC

+0

Cảm ơn Stuart, đã làm việc cho tôi! Bây giờ tôi có {trans.id, FlowDirection = (byte) 1}. Tôi đã tìm ra vấn đề. Khi tôi trừ 1 từ byte, nó đã trở thành một int là tốt. Xem ở đây: http://stackoverflow.com/questions/941584/byte-byte-int-why – ohyeah

Trả lời

4

Chỉ cần để mở rộng trên bình luận:

Surely you can just use two constants (or literals)?, i.e.

from trans in data.Transactions 
join toTrans in data.TransactionDetails 
    on new {ID = trans.id, Flow = (byte)1} 
    equals new {Id = toTrans.tId, Flow = toTrans.FlowDirection} 
join fromTrans in data.TransactionDetails 
    on new { Id = trans.id, Flow = (byte)0} 
    equals new { Id = fromTrans.tId, Flow = fromTrans.FlowDirection } 

Could FlowDirect - 1 not work because it turns FlowDirect into an int instead? Does subtracting an int from a byte turn the byte into an int maybe? Otherwise, I really don't know why your code works.

Vâng, bạn sẽ cần phải đúc kết quả trở lại byte (hoặc đen 1-byte để điều hành byte "-" được sử dụng)

0

How can I change the value of FlowDirection? I can't change the name of the variable or subtract 1 inside the anonymous object

Để thay đổi biến của bạn bên trong đối tượng ẩn danh chỉ cần làm:

new { fromTrans.tId, FlowDirection = fromTrans.FlowDirection - 1 } 

ví dụ.

+0

Đáng buồn thay không, đây là những gì tôi đã cố gắng cũng như trong một số hình thức. Tôi gặp lỗi kiểu tương tự. FlowDirection - = 1 và FlowDirect = toTrans.FlowDirection - 1 không hoạt động. – ohyeah

0

Một ý tưởng khác:

from trans in data.Transactions 
join toTrans in data.TransactionDetails on trans.id equals new toTrans.tId 
join fromTrans in data.TransactionDetails on trans.id equals fromTrans.tId 
where toTrans.FlowDirection == 1 && fromTrans.FlowDirection == 1 

Tôi nghĩ rằng tùy chọn này nên dễ đọc hơn.

Các vấn đề liên quan