adapter predicate example?

Posts   
 
    
mvording
User
Posts: 19
Joined: 04-Oct-2008
# Posted on: 21-Oct-2008 00:56:40   

Hi:

I have a "newbie" question on doing database calls using the dataadapter.

I want to fetch a specific entity from the database using the equivalent of a SQL where clause (not the primary key).

SELECT * FROM SomeEntity WHERE somekey = 234 and somedate >= '2008-10-01'

I saw some documentation on creating the where clause by creating a PredicateExpression, which I was able to add the above with PredicateExpression.Add(...) statements.

However I don't know which method to pass this PredicateExpression to the adapter instance (the Fetch method doesn't seem to have a signature with this ).

Sincerely, Matt

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 21-Oct-2008 04:50:41   

You are to use the IRelationPredicateBucket. This interface contains the actual predicates as well as the relationships needed if there is a multi-table query. See ref. Hopefully this helps!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Oct-2008 05:54:41   

Yes. From your example, the code should looks like:

EntityCollection<SomeEntity> theCollection = new EntityCollection<SomeEntity>(new SomeEntityFactory());

// the filter
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(SomeFields.SomeKey == 234);
bucket.PredicateExpression.Add(SomeFields.SomeDate >= new DateTime(2008, 10, 1));

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
     adapter.FetchEntityCollection(theCollection, bucket);
}

The difference between IRelationPredicateBucket and IPredicateExpression is that the RelationPredicateBucket contains a PredicateExpression and a RelationCollection. So, if you want to filter on a related entity, you have to add that relation to the Relations property of the bucket.

For EntityCollection fetches, Adapter templateSet uses IRelationPredicateBucket, SelfServicing uses IPredicateExpression.

David Elizondo | LLBLGen Support Team