防止 NHibernate 在 ORDER BY 中出现别名

发布于 2024-09-17 06:54:49 字数 578 浏览 5 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-09-24 06:54:49
Projections.SqlFunction("coalesce",
                        NHibernateUtil.String,
                        Projections.Property("x.Company"),
                        Projections.Property("y.Company"));

coalesce(或任何其他)函数的参数应单独传递(实际上,作为 params 数组),而不是合并到 ProjectionList 中。

Projections.SqlFunction("coalesce",
                        NHibernateUtil.String,
                        Projections.Property("x.Company"),
                        Projections.Property("y.Company"));

The parameters to the coalesce (or any other) function should be passed separately (actually, as a params array), not merged into a ProjectionList.

苦行僧 2024-09-24 06:54:49

我也遇到过类似的恼人问题。您可能必须从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文