LINQ - Where not exists

本小妞迷上赌 提交于 2019-11-27 01:27:22

问题


What is the equivalent of following statement in 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 )

回答1:


Try this Not Any pattern.

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



回答2:


Query syntax version of @Amy B's answer (with !Any inverted 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
};



回答3:


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


来源:https://stackoverflow.com/questions/899090/linq-where-not-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!