【问题标题】:Linq Inner join with an Or clause [closed]带有 Or 子句的 Linq 内连接 [关闭]
【发布时间】:2015-08-04 21:48:20
【问题描述】:

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

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

      【解决方案2】:

      JOINOR 只是两个单独查询的 UNIONJOIN。 因此,要使用 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();
      

      【讨论】:

      • 它给了我错误。就像我使用没有 OR 子句和第二个语句的 SQL 查询一样
      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      相关资源
      最近更新 更多