首页 > 解决方案 > 将 Sql all 运算符转换为 linq

问题描述

我需要从应该从另一个表输出中排除的表中检索详细信息。
以下查询成功检索了所需的输出:

select * from "table1" 
where 
"table1"."Id" != All (select "table2"."table1Id" from "table2" where (*conditions*))
AND *conditions*

我想在 linq 中进行相同的查询。我试过但无法实现它,因为我不知道如何在 linq 中使用all 。

from table1 in _db.table1
where
table1.Id != (from table2 in _db.table2 where (*conditions*))
&& *conditions*
select new class() {
                      Id = user.Id,
                      Name = user.Name
                  };  

我已经尝试了很多参考,但对我不起作用。帮助我将此sql语句转换为linq。

标签: c#sqllinq

解决方案


也许使用包含:

from table1 in _context.table1
where
!(from table2 in _context.table2 where (*conditions*) select table2.table1Id).Contains(table1.Id)
&& *conditions*
select new class() {
                  Id = user.Id,
                  Name = user.Name
              };  

推荐阅读