Problem with Negative predicates

Posts   
 
    
LordBauer
User
Posts: 9
Joined: 14-Apr-2008
# Posted on: 14-Apr-2008 19:12:43   

Hello,

I have a problem while using Negative predicates. Two Tables (TblModule, TblModulPortMedikamente) have a 1:m relation. The relation is between the Fields TblModulPortMedikamenteFields.ModulIdDemand and TblModuleFields.ModulId.

I try to create a query which is the same to the sql statement:

SELECT * FROM        tblModule WHERE     (NOT EXISTS
                          (SELECT * FROM          tblmodulportmedikamente
                           WHERE      tblModule.modulid = tblmodulportmedikamente.modulID_demand))

Here my try with LLBLGEN PRO:

 public IList<ModulExchange> GetModuls()
        {
            EntityCollection<TblModuleEntity> moduls =
                    new EntityCollection<TblModuleEntity>(new TblModuleEntityFactory());

            RelationPredicateBucket bucket = new RelationPredicateBucket();

            bucket.Relations.Add(TblModuleEntity.Relations.TblModulPortMedikamenteEntityUsingModulIdDemand);
            
            bucket.PredicateExpression.Add( TblModulPortMedikamenteFields.ModulIdDemand != TblModuleFields.ModulId);

    
            ISortExpression sortExpression = new SortExpression(TblModuleFields.ModulId | SortOperator.Ascending);

            m_adapter.FetchEntityCollection(moduls, bucket, 0, sortExpression);
        
            return Converter.ConvertToIList(moduls);
        }

What is my fault?

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 14-Apr-2008 19:36:13   

your code generates something like this:

SELECT * FROM tblModule INNER JOIN tblmodulportmedikamente ON (tblModule.modulid = tblmodulportmedikamente.modulID_demand) WHERE tblmodulportmedikamente.modulID_demand <> tblModule.modulid ORDER BY
tblModule.modulid ASC

which kind of negates itself, so i guess you need to get your sql straight first.

LordBauer
User
Posts: 9
Joined: 14-Apr-2008
# Posted on: 14-Apr-2008 19:56:54   

Hello,

ok I am understanding. But how can I use the sql Statement NOT EXISTS with LLBLGEN?

Thanks

LordBauer

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Apr-2008 06:50:12   

Yes you can. You should use _FieldCompareSetPredicate _. This is copied from manual (LLBLGenPro Help - using the generated code - Adapter - Filtering and sorting - The predicate system):

FieldCompareSetPredicates also allows you to define EXISTS () queries. It is then not necessary to specify an IEntityField2 object with the predicate's constructor (specify null / nothing) as it is ignored when building the SQL. Keep in mind that EXISTS() queries are semantically the same as IN queries and IN queries are often simpler to formulate.

A FieldCompareSetPredicate's constructor overload receives a _bool negate _parameter. Set it to true and you will obtain a "NOT EXISTS" clause.

David Elizondo | LLBLGen Support Team
LordBauer
User
Posts: 9
Joined: 14-Apr-2008
# Posted on: 15-Apr-2008 08:16:52   

Hello again,

I try this but the result is the same:

bucket.PredicateExpression.Add(new FieldCompareSetPredicate(
                  TblModuleFields.ModulId, null, TblModulPortMedikamenteFields.ModulIdDemand , null,
                  SetOperator.Exist, null, true));

Is there a chance to see the generate sql statement?

Thanks

LordBauer

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Apr-2008 11:51:30   

You need to use something like:

PredicateExpression existsSubqueryFilter = new PredicateExpression(TblModuleFields.ModulId == TblModulPortMedikamenteFields.ModulIdDemand );

bucket.PredicateExpression.Add(
    new FieldCompareSetPredicate(
        null, null,
        TblModulPortMedikamenteFields.ModulIdDemand, null,
        SetOperator.Exist,
        existsSubqueryFilter,
        true)
    );
LordBauer
User
Posts: 9
Joined: 14-Apr-2008
# Posted on: 15-Apr-2008 13:06:20   

Ok now it works.

Thanks for all replys.

LordBauer