2015-01-08 19 views
5

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.

+3

Perché non è possibile utilizzare solo due costanti o semplicemente gettere letterali '(byte) 0' e' (byte) 1'? – StuartLC

+0

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

risposta

4

Solo per espandere il commento:

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.

Sì, si avrebbe bisogno di gettare il risultato di nuovo al byte (o letterale 1-byte in modo che l 'operatore di byte "-" è usato)

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

Per cambiare la variabile all'interno dell'oggetto anonimo semplicemente fare:

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

per esempio.

+0

Purtroppo no, questo è quello che ho provato anche in varie forme. Ottengo lo stesso errore di tipo. FlowDirection - = 1 e FlowDirect = toTrans.FlowDirection - 1 non funzionano neanche. – ohyeah

0

Un'altra idea:

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 

Penso che questa opzione dovrebbe essere più facile da leggere.

Problemi correlati