Help Creating Predicate (with bitwise Expression)

Posts   
 
    
ScottCate
User
Posts: 48
Joined: 11-May-2005
# Posted on: 18-Feb-2007 22:27:49   

I'm trying to create this where clause in SQL Server...

WHERE bitValue & 10 > 0

This will give me any row, that has a bitvalue that mashes with 10 ( 2 or 8 ) -- Simple enough. Now writing the predicate, I'm stuck. I'm sure it's something minor I'm over looking. The documentation builds this example. Which uses IExpression(s), but not BitwiseAnd

// C#
IExpression leftOperand = new Expression(MyEntityFields.Field1, ExOp.Add, MyEntityFields.Field2);
IExpression rightOperand = new Expression(
    MyEntityFields.Field3, ExOp.Mul, MyEntityFields.Field4);
IEntityField2 field = MyEntityFields.Field1.SetExpression(leftOperand);
IPredicate filter = FieldCompareExpressionPredicate(field, null, ExOp.GreaterThan, rightOperand);

// which is equal to:
IPredicate filter = ((MyEntityFields.Field1 + MyEntityFields.Field2) > 
    MyEntityFields.Field3 * MyEntityFields.Field4));

In a dream world, I could use this.

FieldCompareValuePredicate( WhateverFields.BitValue, null, ExOp.BitwiseAnd, 10 );

But FieldCompareValuePredicate isn't compatible with ExOp because the Bit Operators are SQLServer Only.

I can't seem to get anything to work.

I guess the short(er) version is that I'm looking for a predicate example that uses ExOp.BitwiseAnd

Thanks for the help.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Feb-2007 01:05:45   

Scott, I would use this code:

// filter object
IRelationPredicateBucket filter = new RelationPredicateBucket();

// bitValue & 10
IExpression leftOperand = new Expression(BitwiseFields.bitValue, ExOp.BitwiseAnd, 10);          

// field will be used to compare
EntityField2 field = BitwiseFields.bitValue.SetExpression(leftOperand);

// bitValue & 10 > 10
filtro.PredicateExpression.Add(leftOperand >= 10);

hope useful. simple_smile

David Elizondo | LLBLGen Support Team
ScottCate
User
Posts: 48
Joined: 11-May-2005
# Posted on: 19-Feb-2007 01:10:31   

After fixing a few minor typos, this worked great.

IExpression leftOperand = new Expression(WhateverFields.BitValue, ExOp.BitwiseAnd, 10);
EntityField2 field = WhateverFields.BitValue.SetExpression(leftOperand);
filter.PredicateExpression.Add(field >= 10);

Thank you daelmo