Order of evaluation

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 09-Nov-2006 15:29:12   

Hi all,

I am modifying search functionality, I have the code:

Dim newFilter As IPredicateExpression
For i = 0 To splitSearch.Length - 1
    If i = 0 Then
        newFilter = New PredicateExpression(PredicateFactory.Like(SupplierFieldIndex.SupplierName, "%" & splitSearch(i) & "%"))
    Else
        newFilter.AddWithAnd(PredicateFactory.Like(SupplierFieldIndex.SupplierName, "%" & splitSearch(i) & "%"))
    End If
    newFilter.AddWithOr(PredicateFactory.Like(SupplierFieldIndex.SupplierAddressTown, "%" & splitSearch(i) & "%"))
Next

But this doesn't do what I want. I want to have something that acts like this: If (subString 1 matches A OR subString 1 matches B) AND (subString 2 matches A OR subString 2 matches B) ...

But I get something that acts like this: If subString 1 matches A OR (subString 1 matches B AND subString 2 matches A) OR subString 2 matches B) ...

Is there any way to change the order of evaluation of AddWithOr and AddWithAnd?

Thanks,

James

BertS
User
Posts: 89
Joined: 28-Jul-2005
# Posted on: 09-Nov-2006 16:14:44   

You can try this:

Dim newFilter As New PredicateExpression

For i = 0 To splitSearch.Length - 1
    Dim orFilter As New PredicateExpression

    orFilter.Add(PredicateExpression(PredicateFactory.Like(SupplierFieldIndex.SupplierName, "%" & splitSearch(i) & "%"))
    orFilter.AddWithOr(PredicateFactory.Like(SupplierFieldIndex.SupplierAddressTown, "%" & splitSearch(i) & "%"))

    newFilter.AddWithAnd(orFilter)
Next

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 09-Nov-2006 16:27:06   

Thanks Bert, that worked (with a very minor ammendment). My code is now:

Dim newFilter As IPredicateExpression
For i = 0 To splitSearch.Length - 1
    Dim orFilter As New PredicateExpression

    orFilter.Add(PredicateFactory.Like(SupplierFieldIndex.SupplierName, "%" & splitSearch(i) & "%"))
    orFilter.AddWithOr(PredicateFactory.Like(SupplierFieldIndex.SupplierAddressTown, "%" & splitSearch(i) & "%"))

    If i = 0 Then
        newFilter = New PredicateExpression(orFilter)
    Else
        newFilter.AddWithAnd(orFilter)
    End If
Next
BertS
User
Posts: 89
Joined: 28-Jul-2005
# Posted on: 10-Nov-2006 10:07:53   

Just out of curiosity: why do you want to create the new instance in the loop? I can't see any reason for that, and it makes your code complexer as far as I can see.