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.
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
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