SQL 到带有子连接和子查询的 LINQ

SQL to LINQ with sub joins and sub queries

我是 LINQ 的新手,在将此 SQL 转换为 LINQ 时遇到问题。任何人都可以帮助我实现这一目标吗?

Select DISTINCT u.ID from 
(Select x.ID from table1 x where x.Info = 0) u
where u.ID not in 
(select c.ID from table1 c where c.Info = 1)

我有一个 table table 1

ID Info 1 0 1 1 1 0 2 0 3 1 3 0 4 0 5 1

我想要 return 所有 Info = 0 的记录,所以上述 table 的输出将是 2, 4

请分享一些见解

var innerQuery = context.Table1.where(z=>z.Info == 1).Select(z=>z.ID)
var query = context.Table1.where(z=>z.Info == 0 && !innerQuery.Any(x=>x==z.ID)).Select(z=>z.ID)

这将 return 只有所有 INFO=0 的 ID

var query = from t in context.table1
    group t by t.ID
    into g
    where g.All(i => i.INFO == 0)
    select g.Key;

虽然您选择了 Octaviocci 的答案,但我要指出的是 LINQ 看起来 SQL 而不是像他那样使用 Lambda 表达式的 LINQ。当您更熟悉 T-SQL:

时,常规 Linq 语句可能如下所示
var nonEquijoinQuery = (from a in table1
                        let b = from c in table1
                            where c.Info == 1
                            select c.ID
                        where a.Info == 0 && b.Contains(a.ID) == false
                        select a.ID).Distinct();