使用额外变量的 Linq 多重连接条件
Linq multiple join conditions using extra variables
我有这个查询:
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 字节。然而,这给出了错误:
The type of one of the expressions in the join clause is incorrect.
根据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 }
哪个有效!但是,第二个连接需要 FlowDirection 的值为 0 而不是 1。如何更改 FlowDirection 的值?我不能更改变量的名称或在匿名对象中减去 1,否则这很容易。
How can I change the value of FlowDirection? I can't change the name of the variable or subtract 1 inside the anonymous object
要更改匿名对象中的变量,只需执行以下操作:
new { fromTrans.tId, FlowDirection = fromTrans.FlowDirection - 1 }
例如。
只是为了扩展评论:
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.
是的,您需要将结果转换回字节(或将文字 1
转换为 byte
,以便使用字节运算符“-”)
另一个想法:
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
我认为这个选项应该更容易阅读。
我有这个查询:
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 字节。然而,这给出了错误:
The type of one of the expressions in the join clause is incorrect.
根据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 }
哪个有效!但是,第二个连接需要 FlowDirection 的值为 0 而不是 1。如何更改 FlowDirection 的值?我不能更改变量的名称或在匿名对象中减去 1,否则这很容易。
How can I change the value of FlowDirection? I can't change the name of the variable or subtract 1 inside the anonymous object
要更改匿名对象中的变量,只需执行以下操作:
new { fromTrans.tId, FlowDirection = fromTrans.FlowDirection - 1 }
例如。
只是为了扩展评论:
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.
是的,您需要将结果转换回字节(或将文字 1
转换为 byte
,以便使用字节运算符“-”)
另一个想法:
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
我认为这个选项应该更容易阅读。