我是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)
我有一个表表1
ID Info
1 0
1 1
1 0
2 0
3 1
3 0
4 0
5 1
我想返回所有 Info = 0 的记录,因此上表的输出将是2, 4
请分享一些见解
这将
仅返回所有 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的答案,但我要指出的是,有看起来像SQL的LINQ,而不是像他一样使用Lambda表达式的LINQ。下面是常规 Linq 语句在您更熟悉 T-SQL 时的样子:
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();
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)