tags:

views:

3464

answers:

2

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 )
+7  A: 

Try this Not Any pattern.

var query = db.table1
.Where(t1 => !db.table2
  .Any(t2 => t2.cat == t1.cat && t2.julianDte < t1.julianDte)
);
David B
+3  A: 

Query syntax version of @David 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
};
Bryan Watts
how well does that translate to sql? In my experience, "OR" is to be avoided.
David B
Out of curiosity, what about "OR" would make it more dangerous than "AND"?
Bryan Watts
OR tends to interfere with the use of indexes. Find people who live on 3rd street AND are plumbers... In this case, an index of people by the street they live on is highly useful. Find people who live on 3rd street OR are plumbers... In this case, an index of people by the street they live on is much less useful.
David B