防止 NHibernate 在 ORDER BY 中出现别名
我有一个包含 Order By 子句的查询。从 NHibernate 生成的 SQL 看起来像
ORDER BY coalesce(x.Company as x__.Company, y.Company) asc
这样失败,因为 MS SQL Server 中的 Order by 子句中不允许使用“as”。有什么办法可以防止锯齿吗?
我编写的条件查询如下所示:
var orderBy = Projections.SqlFunction("coalesce", NHibernateUtil.String,
Projections.ProjectionList()
.Add(Projections.Property("x.Company"))
.Add(Projections.Property("y.Company")));
var order = Order.Asc(orderBy);
criteria.AddOrder(order);
I have a query which has an Order By clause. The generated SQL from NHibernate looks like
ORDER BY coalesce(x.Company as x__.Company, y.Company) asc
This fails as 'as' is not allowed in Order by clause in MS SQL Server. Is there any way I can prevent aliasing?
The criteria query that I have written looks like:
var orderBy = Projections.SqlFunction("coalesce", NHibernateUtil.String,
Projections.ProjectionList()
.Add(Projections.Property("x.Company"))
.Add(Projections.Property("y.Company")));
var order = Order.Asc(orderBy);
criteria.AddOrder(order);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
coalesce(或任何其他)函数的参数应单独传递(实际上,作为 params 数组),而不是合并到 ProjectionList 中。
The parameters to the coalesce (or any other) function should be passed separately (actually, as a params array), not merged into a ProjectionList.
我也遇到过类似的恼人问题。您可能必须从 PropertyProjection 派生一个类并使用它来代替 Projections.Property()。重写 ToSqlString 方法并删除 AS 子句。
I've had similar annoying problems. You might have to derive a class from PropertyProjection and use that inplace of Projections.Property(). Override the ToSqlString method and strip out the AS clause.