【问题标题】:LINQ Left Join On Not Equal Rows不相等行上的 LINQ 左连接
【发布时间】:2010-11-17 06:54:34
【问题描述】:

我正在尝试使用 LINQ 显示其他表中不存在的行。谁能帮帮我?

这是我使用的sql。

select * from table1 
left join table2
on 
table1.col1 = table2.col1 
and 
table1.col2 = table2.col2
where
table2.col1 is null and table2.col2 is null

已经搜索并找到了一些解决方案。这是我到目前为止所做的。

from t1 in table1
where 
!(from t2 in table1
  join t3 in table2 on
  new { t2.col1, t2.col2 } 
  equals 
  new { t3.col1, t3.col2 }
  select t2.PK).Contains(t1.PK)
  select t1

上面的代码运行良好,但我只是想知道这是否是我可以使用的唯一解决方案?我的意思是,我们不能直接使用带有 where 子句的 left join linq,而不是使用 JOIN 和 CONTAINS 方法吗?

【问题讨论】:

    标签: linq join inequality


    【解决方案1】:

    好吧,你可以这样做:

    var query = from t1 in table1
                join t2 in table2
                on new { t1.col1, t2.col2} equals { t2.col1, t2.col2 }
                into groups
                where !groups.Any()
                select t1;
    

    这里,groupst2 中与“当前”t1 匹配的行集 - 如果没有任何组,它将为空,这正是您想要的。检查序列是否为空的最简单方法是使用Any 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2017-04-10
      相关资源
      最近更新 更多