ho questa query:Linq multipla unirsi condizioni utilizzando variabili aggiuntive
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
Che ho cercato di ricreare utilizzando tipi anonimi, come spiegato 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 }
La direzione del flusso è sempre 1 o 0, quindi sto utilizzando il byte toflow. Questo, tuttavia, dà l'errore:
The type of one of the expressions in the join clause is incorrect.
Secondo this risposta, sia il nome e il tipo devono corrispondere. Il che significherebbe:
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 }
Quale funziona! Tuttavia, il secondo join deve avere una FlowDirection di valore 0 anziché 1. Come posso modificare il valore di FlowDirection? Non posso cambiare il nome della variabile o sottrarre 1 all'interno dell'oggetto anonimo, altrimenti sarebbe stato facile.
Perché non è possibile utilizzare solo due costanti o semplicemente gettere letterali '(byte) 0' e' (byte) 1'? – StuartLC
Grazie Stuart, ha funzionato per me! Ora ho {trans.id, FlowDirection = (byte) 1}. Ho capito qual è il problema. Quando ho sottratto 1 dal byte, è diventato anche un int. Vedi qui: http://stackoverflow.com/questions/941584/byte-byte-int-why – ohyeah