ID_A data_A
1 a
2 b
表B
ID_B data_B ID_A
1 2 1
2 3 2
表c
ID_C data_C ID_A ID_B
1 1 1 1
我的linq查询
var data = (from a in _db.Table_A
join b in db.Table_B
on a.ID_A equals b.ID_A
join c in _db.Table_C
on new { ID_A = a.ID_A, ID_B = b.ID_B} equals new { ID_A = c.ID_A, ID_B = c.ID_B }
select a)
在连接table_C时,连接显示错误(连接子句中某个表达式的类型不正确。在要加入的cal中类型推断失败)。
我想在table_C join的连接条件中同时使用table_A和Table_B id
答案 0 :(得分:1)
您的查询应该像
var data = (from a in _db.Table_A
join b in _db.Table_B on a.ID_A equals b.ID_A
join c in _db.Table_C on new { a.ID_A,b.ID_B} equals new { c.ID_A,c.ID_B }
select a)
如果表C中没有nullable
值,这里有demo
如果table_C
中的某个字段为nullable
,则此处为解决问题的解决方法
var data = (from a in _db.Table_A()
join b in _db.Table_B() on a.ID_A equals b.ID_A
join c in _db.Table_C() on new { m=a.ID_A,n=(int?)b.ID_B} equals new { m=c.ID_A,n=c.ID_B }
select a);
工作demo
希望它会帮助你