LINQ - 哪里不存在

时间:2009-05-22 17:47:04

标签: linq exists

LINQ中以下语句的等价物是什么:

Select t1.appname, t1.julianDte, t1.cat 
From table1 t1 
Where NOT EXISTS 
   ( Select * 
     from table t2 
     where t1.cat = t2.cat AND t2.julianDte < t1.julianDte )

3 个答案:

答案 0 :(得分:59)

试试这个Not Any模式。

var query = db.table1
.Where(t1 => !db.table2
  .Any(t2 => t2.cat == t1.cat && t2.julianDte < t1.julianDte)
);

答案 1 :(得分:11)

查询@David B答案的语法版本(使用!Any to all to All):

from t1 in db.Table1
where db.Table2.All(t2 => t1.cat != t2.cat || t2.julianDte >= t1.julianDte)
select new
{
    t1.appname,
    t1.julianDte,
    t1.cat
};

答案 2 :(得分:1)

from t1 in Context.table1DbSet
let ok = 
    (from t2 in Context.table2DbSet 
     where t2.Idt1 = t1.Idt1 && t1.DateValid.HasValue
    ).Any()
where 
   t1.Active
   && !ok