开发者

Prevent NHibernate from aliasing in ORDER BY

I have a query which has an Order By clause. The generated SQL fro开发者_Go百科m 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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜