Generating Filters from Table Values...

Posts   
 
    
hlesesne avatar
hlesesne
User
Posts: 47
Joined: 22-Jul-2004
# Posted on: 13-Aug-2004 19:32:28   

I am trying my hardest to get my brain around using predicate expressions correctly and wondered if anyone could offer any insight on accomplishing the following...

Given the following Entities:

Letters Content City State Zip Type PostalType

Criteria Field Value

I want to extract all of the Letters that I need to send based on the content of my CriteriaEntityCollection.

Let's say I have two CriteriaEntities in the Collection

So if Criteria(0).Field = "State" and Criteria(0).Value="NC" and Criteria(1).Field="Type" and Criteria(1).Value="NewCustomer", how would I build a LettersEntityCollection that meets the following criteria (SQL Psudo):

" Where State = 'NC' AND Type = 'NewCustomer' "

I need to some way to generate the predicate values on the fly at runtime. I am assuming it will take a bunch of loops through the field indexes, but am wondering if there is a more simple solution i.e. generate some type of where clause to filter the LettersEntityCollection.

My idea is the following - just psudo - not necessarily using valid methods/properties:

For each Criteria in CriteriaCollection for each FieldIndex in LettersFields if LetterFields(FieldIndex).tostring = Criteria.Field.tostring then AddPredicate(PredicateFactory.CompareValue(LetterFields(FieldIndex),equals,Criteria.Value) end if next next

Any suggestions would be greatly appreciated.

Thanks,

Hal

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 14-Aug-2004 13:59:11   

something like:


Dim dummyLetter As New LetterEntity()
Dim filter As New PredicateExpression()

For each currentCriterium As Criteria in CriteriaCollection
    filter.Add(New FieldCompareValuePredicate(dummyLetter.Fields(currentCriterium.Field), _
        ComparisonOperator.Equal, currentCriterium.Value))
next

All that matters is that you pass in field objects to the predicate objects because these are used to determine which fields to filter on, nothing more. The value is passed in separately.

This code might not compile due to vb.net syntax errors, but you get the idea simple_smile

Frans Bouma | Lead developer LLBLGen Pro