views:

369

answers:

1

hi i am new in NHibernate and i am a little confused.

Suppose we have a product table. Let the product table have 2 columns price1 and price2.

then i may query mapped product entities via HQL as follows:

string queryString = @"from product p
where p.price1 = p.price2 + 100 ";
IList result = session.CreateQuery(queryString).List();

How can i achieve that via ICriteria API.

i know it's absurd but i am trying sth like that:

session.CreateCriteria(typeof(product))
   .Add(Expression.Eq("price1", "price2" + 100))
   .List()

or more appropriately sth like that (using lambda extensions):

session.CreateCriteria(typeof(product))
   .Add<product>(p => p.price1 == (p.price2 + 100))
   .List()

in fact i downloaded lambda extensions project from googlecode and i extended it to recusively process Binary and Unary expressions to implement expresssions like:

session.CreateCriteria(typeof(product))
   .Add<product>(p => 
               (!(p.price1 > 29007) && (p.price1 > 19009)) 
            || (p.price2 == 29009));

i am currently processing queries like above but the arithmethic clauses are annoying me since i can't return the appropriate Restriction to obtain the Criterion i need.

mpffh i m tired of trying to explain it in a comprehensive way. i hope it worked. Any help is appreciated.

+1  A: 

You could try this:

ISQLFunction sqlAdd = new VarArgsSQLFunction("(", "+", ")");
var products = session
    .CreateCriteria<product>()
    .Add(
        Expression.EqProperty(
            "price1", 
            Projections.SqlFunction(
                sqlAdd, 
                // TODO: replace this with appropriate type
                NHibernateUtil.Double,
                Projections.Property("price2"),
                Projections.Constant(100) 
            )
        )
    )
    .List<product>();
Darin Dimitrov
ty very much, that dialect translation worked perfect.In fact, it also gave me another clue to process MethodCallExpressions such as =>session.CreateCriteria(typeof(product)) .Add<product>(p => p.price1.ToString() == (p.price2 + 100).ToString()) .List()for exemple, extending your way, i am know able to create eqProperty criterion between 2 columns of type NUMBER and VARCHAR2 using ToString() in the expressions. 2 in a row...
mutoss