【发布时间】:2015-03-06 13:13:27
【问题描述】:
我有这个问题:
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
我尝试使用匿名类型重新创建,如 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 总是 1 或 0,所以我使用 toFlow 字节。然而,这给出了错误:
join 子句中的其中一个表达式的类型不正确。
根据this的回答,名称和类型都需要匹配。这意味着:
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 }
哪个有效!但是,第二个连接需要有一个值为 0 而不是 1 的 FlowDirection。如何更改 FlowDirection 的值?我不能更改变量的名称或在匿名对象中减去 1,否则这很容易。
【问题讨论】:
-
为什么不能只使用两个常量,或者只转换文字
(byte)0和(byte)1? -
谢谢斯图尔特,这对我有用!我现在有 {trans.id, FlowDirection = (byte)1}。我解决了这个问题。当我从字节中减去 1 时,它也变成了一个 int。见这里:stackoverflow.com/questions/941584/byte-byte-int-why