问题
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