Nhibernate: restriction with sum of 2 columns

江枫思渺然 提交于 2019-12-23 17:08:05

问题


Can I create this sql query using HNibernate Criteria:

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

All 3 columns are int32. Thanks


回答1:


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")
                                                  )
                )
);



回答2:


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

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


来源:https://stackoverflow.com/questions/4243890/nhibernate-restriction-with-sum-of-2-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!