How do I compare an expression to a constant value

Posts   
 
    
danh
User
Posts: 70
Joined: 16-Jul-2007
# Posted on: 23-Sep-2008 13:32:06   

Hi,

I am struggling a bit trying to compare an expression to a constant value, as in the following SQL query:


SELECT *
FROM property p
INNER JOIN currency c ON c.ID = p.CurrencyID
WHERE (p.Price / c.ExRate) > 1000

I have tried the following (adapted from the manual, and a bit of a stab in the dark..) but it doesn't work:


IExpression leftOperand = new Expression(PropertyFields.Price, ExOp.Div, CurrencyFields.ExRate);
IExpression rightOperand = new Expression(1000, ExOp.None, (IEntityFieldCore)null);
IEntityField2 field = PropertyFields.Price.SetExpression(leftOperand);
IPredicate priceFilter = new FieldCompareExpressionPredicate(field, null, ComparisonOperator.GreaterEqual, rightOperand);

filter.PredicateExpression.Add(priceFilter);

Searching the forums, I thought I'd found my answer in: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=8765

...but the suggestion given there won't compile:


IPredicate filter = ((PropertyFields.Price / CurrencyFields.ExRate) > 1000);

The compiler says "Operator '>' cannot be applied to operands of type 'SD.LLBLGen.Pro.ORMSupportClasses.Expression' and 'int'"

I feel like I'm missing something obvious here but whatever combination of expression and field objects I combine I can't quite get it. If anybody knows how to do this I'd be most grateful!

I'm using 2.6 Final, adapter, .NET 3.5, MySql 5.0

Thanks in advance, Dan

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 23-Sep-2008 15:50:13   

Try the following:

IExpression exp = new Expression(PropertyFields.Price, ExOp.Div, CurrencyFields.ExRate);

IEntityField2 field = PropertyFields.Price.SetExpression(exp);

filter.PredicateExpression.Add(field > 1000);
danh
User
Posts: 70
Joined: 16-Jul-2007
# Posted on: 23-Sep-2008 16:42:57   

That does that trick...almost. I still get the same compilation error. But if I declare 'field' as an EntityField2 instead of an IEntityField2 it compiles fine.


IExpression exp = new Expression(PropertyFields.Price, ExOp.Div, CurrencyFields.ExRate);

EntityField2 field = PropertyFields.Price.SetExpression(exp);

filter.PredicateExpression.Add(field > 1000);

Thanks for the help, Walaa! smile

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 23-Sep-2008 17:14:03   

You are welcomed smile