Best way to write DoesRowMatchingCriteriaExist query

Posts   
 
    
saggett
User
Posts: 50
Joined: 12-Nov-2007
# Posted on: 20-Aug-2008 17:25:40   

I'm using Adapter on .Net 3.5, June 6th Release.

I'm trying to figure out the most efficient way to get a bool result stating whether or not there is a row in a db table matching the criteria expressed by a RelationPredicateBucket. At the moment I've got:

public bool EntityMatchingCriteriaExists(IEntityFactory2 factory, IRelationPredicateBucket criteria)
        {
            var collection = new EntityCollection(factory);
            var adapter = CreateAdapter();
            return adapter.GetDbCount(collection, criteria) > 0;
        }

But this is sub-optimal as I don't need the count, I just need to find out whether there's anything matching that criteria in the table.

Any ideas?

Thanks, Stephen

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Aug-2008 21:31:31   

Hi Stephen,

I think _EXISTS _if at lest 36% more efficient than COUNT(*) in this case:

public bool EntityMatchingCriteriaExists(IEntityFactory2 factory, IRelationPredicateBucket criteria)
{
    var collection = new EntityCollection(factory);
    var adapter = CreateAdapter();

    IRelationPredicateBucket newFilter = new RelationPredicateBucket();
    FieldCompareSetPredicate existsPredicate = new FieldCompareSetPredicate(null, null, 
        factory.CreateFields()[0], null, SetOperator.Exist, 
        criteria.PredicateExpression, criteria.Relations, "", 1, null, false, null);

    newFilter.PredicateExpression.Add(existsPredicate);
    
    adapter.FetchEntityCollection(collection, newFilter, 1);

    return collection.Count > 0;
}
David Elizondo | LLBLGen Support Team