문제

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);
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top