使用QueryOver这有点问题,因为我们在使用SQL函数时需要使用Projections。
我放弃了abs(...)
,因为它时并不需要,因为x * x == -x * -x
选项1:使用SqlProjection(不漂亮,但更短)
int x = 10;
int y = 20;
var result = session.QueryOver<Product>()
.OrderBy(Projections.SqlProjection(
@"power(x - " + x.ToString() + ", 2) + power(y - " + y.ToString() + ", 2) as tmporder",
new string[] { "test" },
new NHibernate.Type.IType[] { NHibernateUtil.Int32 })).Asc
.List();
选项2:使用SqlFunction - 这是长并需要看看下面的链接:
这样的事情(你需要包括从链接OperatorProjection;它不完整,我没有测试它)
.OrderBy(Projections.SqlFunction("power", NHibernateUtil.Double,
new ArithmeticOperatorProjection("+", NHibernateUtil.Int32,
Projections.Property<Product>(p => p.x), Projections.Constant(x)),
Projections.Constant(2))).Asc
NHibernate and the missing OperatorProjection Part 1
可悲的是,我得到'当前方言NHibernate.Dialect.MsSql2008Dialect不支持的功能:power' ..有什么解决办法或我要使它成为一个*一个? – 2011-05-23 15:04:34
@ŁukaszW.pl这很奇怪。我使用SQL 2008数据库和NH 3.1测试了选项1,它没有给出错误警告。你使用的是什么版本的NHibernate? – 2011-05-24 10:04:10
最新的可能,但不要担心..我的工作周围..感谢您的帮助;) – 2011-06-01 12:31:06