sei*_*red 3 linq inequality join
我试图使用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
Run Code Online (Sandbox Code Playgroud)
已经搜索并找到了一些解决方案.这是我到目前为止所做的.
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
Run Code Online (Sandbox Code Playgroud)
上面的代码运作良好,但我只是想知道这是否是我可以使用的唯一解决方案?我的意思是,不是使用JOIN和CONTAINS方法,我们不能直接使用left join linq和where子句吗?
好吧,你可以这样做:
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;
Run Code Online (Sandbox Code Playgroud)
这里groups是t2与"当前"匹配的行集t1- 如果没有任何组,它将为空,这正是您想要的.检查序列是否为空的最简单方法是使用该Any方法.
| 归档时间: |
|
| 查看次数: |
4213 次 |
| 最近记录: |