Lullaby
Lullaby

Reputation: 437

Nhibernate: restriction with sum of 2 columns

Can I create this sql query using HNibernate Criteria:

Select * from Table1 where Column1 > (Column2 + Column3)

All 3 columns are int32. Thanks

Upvotes: 3

Views: 2764

Answers (2)

Jaguar
Jaguar

Reputation: 5958

Well, after reading for the n-th time a question with this exact problem i decided to write an implementation that doesn't include writing SQL.

You can check the implementation at http://savale.blogspot.com/2011/04/nhibernate-and-missing.html with which you can write:

criteria.Add(
   Restrictions
     .GeProperty("Prop1",
                 new ArithmeticOperatorProjection("+",
                                 NHibernateUtil.Int32,
                                 Projections.Property("Prop2"), Projections.Property("Prop3")
                                                  )
                )
);

Upvotes: 4

shanabus
shanabus

Reputation: 13115

You can use an Expression and write some SQL, that's what works for me.

criteria.Add(Expression.Sql("Column1 > (Column2 + Column3)"));

Upvotes: 1

Related Questions