Need help translating a query to linq.

Posts   
 
    
scaless
User
Posts: 22
Joined: 28-Mar-2011
# Posted on: 28-Apr-2011 22:44:53   

Hello,

I have a query with an inline query, which I am not sure how to translate into a linq query. I am trying to get a list of items from a table, where this is no foreign key entry in a second cross reference table.

Here is the query.


select *
from table1
where not exists (
   select *
   from table2
   where table1.PK = table2.PK_table1)

Any help would be appreciated.

Thanks, Shaun

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Apr-2011 02:54:28   

Hi Shaun,

You should use the FieldCompareSetPredicate:

// create the filter
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(new FieldCompareSetPredicate(
    null, null, Table2Fields.Pk, null,
    SetOperator.Exists, (Table2Fields.Pk == Table1Fields.Pk)));

// fetch
EntityCollection<Table1Entity> toFetch = new EntityCollection<Table1Entity>();
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
     adapter.FetchEntityCollection(toFetch, bucket);
}
David Elizondo | LLBLGen Support Team
scaless
User
Posts: 22
Joined: 28-Mar-2011
# Posted on: 29-Apr-2011 17:26:30   

Thanks a bunch! For a simple query in SQL this was just stumping me on how to do in code. simple_smile