Linq Inner与Or子句连接

时间:2015-05-22 18:57:06

标签: c# sql sql-server linq inner-join

我有一个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 

2 个答案:

答案 0 :(得分:1)

带有JOIN

OR只是UNION两个单独查询JOIN的{​​{1}}。 因此,要使用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();

答案 1 :(得分: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();