EF 核心加入多个 OR 条件
EF core join with multiple OR conditions
我想将此 sql 查询转换为 linq :
select * from A a
join B b on ( (a.level1= b.PerimeterID and b.PerimeterLevelID = 1)
OR (a.level2= b.PerimeterID and b.PerimeterLevelID = 2)
OR (a.level3= b.PerimeterID and b.PerimeterLevelID = 3)
)
我尝试了什么:
from a in A
join b in B on new {PerimeterID = a.level1, PerimeterLevelID = 1 } equals new { b.PerimeterID, b.PerimeterLevelID }
where (a.level2 == b.PerimeterID && b.PerimeterLevelID == 2) ||
(a.level3 == b.PerimeterID && b.PerimeterLevelID == 3)
它生成此 sql 查询:
select * from A a
join B b on (a.level1= b.PerimeterID and b.PerimeterLevelID = 1)
where (a.level2= b.PerimeterID and b.PerimeterLevelID = 2)
OR (a.level3= b.PerimeterID and b.PerimeterLevelID = 3))
我也试过 Linqer 但它无法将 OR 转换为 linq
SQL cannot be converted to LINQ: OR
我需要做什么才能转换此 sql 查询
你可以试试 DefaultIfEmpty()
。使用此命令连接将表现得像左连接
from a in A
from b in B.DefaultIfempty()
where ( (a.level1== b.PerimeterID && b.PerimeterLevelID == 1)
|| (a.level2== b.PerimeterID && b.PerimeterLevelID == 2)
|| (a.level3== b.PerimeterID && b.PerimeterLevelID == 3))
内部联接等同于过滤交叉联接。那么
怎么样
from a in A
from b in B
where (a.level1 == b.PerimeterID && b.PerimeterLevelID == 1) ||
(a.level2 == b.PerimeterID && b.PerimeterLevelID == 2) ||
(a.level3 == b.PerimeterID && b.PerimeterLevelID == 3)
select new {a,b};
?
我认为这个查询会有所帮助
var query = from a in context.A
join b1 in context.B
on new { Perimeter = a.level1, LevelID = 1 }
equals new { Perimeter = b1.PerimeterID, LevelID = b1.PerimeterLevelID }
join b2 in context.B
on new { Perimeter = a.level2, LevelID = 2 }
equals new { Perimeter = b2.PerimeterID, LevelID = b2.PerimeterLevelID }
join b3 in context.B
on new { Perimeter = a.level3, LevelID = 3 }
equals new { Perimeter = b3.PerimeterID, LevelID = b3.PerimeterLevelID }
select new
{
a,
b1,
b2,
b3
};
我想将此 sql 查询转换为 linq :
select * from A a
join B b on ( (a.level1= b.PerimeterID and b.PerimeterLevelID = 1)
OR (a.level2= b.PerimeterID and b.PerimeterLevelID = 2)
OR (a.level3= b.PerimeterID and b.PerimeterLevelID = 3)
)
我尝试了什么:
from a in A
join b in B on new {PerimeterID = a.level1, PerimeterLevelID = 1 } equals new { b.PerimeterID, b.PerimeterLevelID }
where (a.level2 == b.PerimeterID && b.PerimeterLevelID == 2) ||
(a.level3 == b.PerimeterID && b.PerimeterLevelID == 3)
它生成此 sql 查询:
select * from A a
join B b on (a.level1= b.PerimeterID and b.PerimeterLevelID = 1)
where (a.level2= b.PerimeterID and b.PerimeterLevelID = 2)
OR (a.level3= b.PerimeterID and b.PerimeterLevelID = 3))
我也试过 Linqer 但它无法将 OR 转换为 linq
SQL cannot be converted to LINQ: OR
我需要做什么才能转换此 sql 查询
你可以试试 DefaultIfEmpty()
。使用此命令连接将表现得像左连接
from a in A
from b in B.DefaultIfempty()
where ( (a.level1== b.PerimeterID && b.PerimeterLevelID == 1)
|| (a.level2== b.PerimeterID && b.PerimeterLevelID == 2)
|| (a.level3== b.PerimeterID && b.PerimeterLevelID == 3))
内部联接等同于过滤交叉联接。那么
怎么样from a in A
from b in B
where (a.level1 == b.PerimeterID && b.PerimeterLevelID == 1) ||
(a.level2 == b.PerimeterID && b.PerimeterLevelID == 2) ||
(a.level3 == b.PerimeterID && b.PerimeterLevelID == 3)
select new {a,b};
?
我认为这个查询会有所帮助
var query = from a in context.A
join b1 in context.B
on new { Perimeter = a.level1, LevelID = 1 }
equals new { Perimeter = b1.PerimeterID, LevelID = b1.PerimeterLevelID }
join b2 in context.B
on new { Perimeter = a.level2, LevelID = 2 }
equals new { Perimeter = b2.PerimeterID, LevelID = b2.PerimeterLevelID }
join b3 in context.B
on new { Perimeter = a.level3, LevelID = 3 }
equals new { Perimeter = b3.PerimeterID, LevelID = b3.PerimeterLevelID }
select new
{
a,
b1,
b2,
b3
};