Linq Inner 加入 Or 子句
Linq Inner join with an Or clause
我有一个 SQL 查询,我想将其转换为 Linq,但我无法弄明白。任何帮助将不胜感激。谢谢
SELECT Distinct Name FROM Context
inner join Nodes_1 on
Context.Node1Id= Nodes_1.Id OR
Context.Node2Id= Nodes_1.Id
where Context.ContextId = 1
JOIN
和 OR
只是两个单独查询 JOIN
的 UNION
。
因此,要使用 LINQ 重现它,您应该连接两个 linq 查询。
或者您可以这样做:
var query = (from entry in Context
from node in Nodes_1
where node.Id==entry.Node1Id ||
node.Id==entry.Node2Id
where entry.ContextId==1
select entry.Name)
.Distinct();
这应该适合你:
var something = (from a in Context.Where(i => i.ContextId == 1)
from b in Nodes_1
where (a.Node1Id == b.Id) || (a.Node2Id == b.Id)
select a.Name).Distinct();
我有一个 SQL 查询,我想将其转换为 Linq,但我无法弄明白。任何帮助将不胜感激。谢谢
SELECT Distinct Name FROM Context
inner join Nodes_1 on
Context.Node1Id= Nodes_1.Id OR
Context.Node2Id= Nodes_1.Id
where Context.ContextId = 1
JOIN
和 OR
只是两个单独查询 JOIN
的 UNION
。
因此,要使用 LINQ 重现它,您应该连接两个 linq 查询。
或者您可以这样做:
var query = (from entry in Context
from node in Nodes_1
where node.Id==entry.Node1Id ||
node.Id==entry.Node2Id
where entry.ContextId==1
select entry.Name)
.Distinct();
这应该适合你:
var something = (from a in Context.Where(i => i.ContextId == 1)
from b in Nodes_1
where (a.Node1Id == b.Id) || (a.Node2Id == b.Id)
select a.Name).Distinct();