Typed list question

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 19-Jan-2005 15:16:33   

How can I use a field index in a Predicate expression

I.E. instead of doing this

myExpression.Add(PredicateFactory.CompareValue(UserPreferenceFieldIndex.UserID, ComparisonOperator.Equal, intParentID));

I want to do this

UserPreferenceEntity mEntity = new UserPreferenceEntity();
myExpression.Add(PredicateFactory.CompareValue(mEntity.Fields[3].FieldIndex, ComparisonOperator.Equal, intParentID));

because the field with ID of 3 is the User ID column

This obviously does not work but is there a way - what am I doing wrong?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Jan-2005 17:12:36   

hplloyd wrote:

How can I use a field index in a Predicate expression

I.E. instead of doing this

myExpression.Add(PredicateFactory.CompareValue(UserPreferenceFieldIndex.UserID, ComparisonOperator.Equal, intParentID));

I want to do this

UserPreferenceEntity mEntity = new UserPreferenceEntity();
myExpression.Add(PredicateFactory.CompareValue(mEntity.Fields[3].FieldIndex, ComparisonOperator.Equal, intParentID));

because the field with ID of 3 is the User ID column

This obviously does not work but is there a way - what am I doing wrong?

The PredicateFactory uses the enum types. This means that the actual enum type has to be passed in, you're passing in an integer.

However this is easily circumvented, as the PredicateFactory under the hood creates a FieldCompareValuePredicate instance, which accepts an EntityField. I'm not sure if you're using adapter or selfservicing, but here are the statements you can use:

selfservicing:

UserPreferenceEntity mEntity = new UserPreferenceEntity();
myExpression.Add(new FieldCompareValuePredicate(
    mEntity.Fields[3], 
    ComparisonOperator.Equal, 
    intParentID));

adapter:

UserPreferenceEntity mEntity = new UserPreferenceEntity();
myExpression.Add(new FieldCompareValuePredicate(
    mEntity.Fields[3], 
    null,
    ComparisonOperator.Equal, 
    intParentID));

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 24-Jan-2005 09:34:11   

Fantastic Thanks