@@ -156,11 +156,12 @@ private static void AppendSortExpressions(Dictionary<SqlString, SqlString> alias
156156 }
157157
158158 SqlString sortExpression = RemoveSortOrderDirection ( sortExpressions [ i ] ) ;
159- if ( aliasToColumn . ContainsKey ( sortExpression ) )
159+ SqlString s ;
160+ if ( aliasToColumn . TryGetValue ( sortExpression , out s ) )
160161 {
161- result . Add ( aliasToColumn [ sortExpression ] ) ;
162+ result . Add ( s ) ;
162163 }
163- else
164+ else
164165 {
165166 result . Add ( sortExpression ) ;
166167 }
@@ -196,21 +197,25 @@ private static void AppendSortExpressionsForDistinct(Dictionary<SqlString, SqlSt
196197 if ( sortExpression . StartsWithCaseInsensitive ( "CURRENT_TIMESTAMP" ) )
197198 result . Add ( sortExpression ) ;
198199
199- else if ( columnToAlias . ContainsKey ( sortExpression ) )
200- {
201- result . Add ( "q_." ) ;
202- result . Add ( columnToAlias [ sortExpression ] ) ;
203- }
204- else if ( columnToAlias . ContainsValue ( sortExpression ) ) // When a distinct query is paged the sortexpressions could already be aliased.
205- {
206- result . Add ( "q_." ) ;
207- result . Add ( sortExpression ) ;
208- }
209200 else
210201 {
211- throw new HibernateException (
212- "The dialect was unable to perform paging of a statement that requires distinct results, and "
213- + "is ordered by a column that is not included in the result set of the query." ) ;
202+ SqlString value ;
203+ if ( columnToAlias . TryGetValue ( sortExpression , out value ) )
204+ {
205+ result . Add ( "q_." ) ;
206+ result . Add ( value ) ;
207+ }
208+ else if ( columnToAlias . ContainsValue ( sortExpression ) ) // When a distinct query is paged the sortexpressions could already be aliased.
209+ {
210+ result . Add ( "q_." ) ;
211+ result . Add ( sortExpression ) ;
212+ }
213+ else
214+ {
215+ throw new HibernateException (
216+ "The dialect was unable to perform paging of a statement that requires distinct results, and "
217+ + "is ordered by a column that is not included in the result set of the query." ) ;
218+ }
214219 }
215220
216221 if ( sortExpressions [ i ] . Trim ( ) . EndsWithCaseInsensitive ( "desc" ) )
0 commit comments