我有一个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
这应该可以为您工作:
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();
JOIN
与OR
只是UNION
与JOIN
的两个独立查询。因此,要用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();