File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -303,13 +303,19 @@ protected virtual void HandleOrderBy()
303303 protected string ToOrderByDirective ( OrderByClause item )
304304 {
305305 string name ;
306- if ( _columns . Any ( r => ( ! string . IsNullOrWhiteSpace ( r . GetAlias ( ) ) ) && r . GetAlias ( ) . Equals ( item . Reference . GetName ( ) ) ) )
306+ if ( ! string . IsNullOrWhiteSpace ( item . Reference . GetOwner ( ) . GetAlias ( ) ) )
307+ {
308+ name = string . Format ( "{0}.{1}" , _schema . QuoteObjectName ( item . Reference . GetOwner ( ) . GetAlias ( ) ) ,
309+ _schema . QuoteObjectName ( item . Reference . GetName ( ) ) ) ;
310+ }
311+ else if ( _columns . Any ( r => ( ! string . IsNullOrWhiteSpace ( r . GetAlias ( ) ) ) && r . GetAlias ( ) . Equals ( item . Reference . GetName ( ) ) ) )
307312 {
308313 name = item . Reference . GetName ( ) ;
309314 }
310- else
311- {
312- name = _table . FindColumn ( item . Reference . GetName ( ) ) . QualifiedName ;
315+ else
316+ {
317+ var table = _schema . FindTable ( item . Reference . GetOwner ( ) . GetName ( ) ) ;
318+ name = table . FindColumn ( item . Reference . GetName ( ) ) . QualifiedName ;
313319 }
314320
315321 var direction = item . Direction == OrderByDirection . Descending ? " DESC" : string . Empty ;
Original file line number Diff line number Diff line change 11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Linq ;
45using NUnit . Framework ;
56
@@ -427,6 +428,20 @@ public void SelfJoinShouldNotThrowException()
427428
428429 Assert . AreEqual ( 1 , kingsSubordinates . Count ) ;
429430 }
431+
432+ [ Test ]
433+ public void OrderByOnJoinedColumnShouldUseJoinedColumn ( )
434+ {
435+ var traceListener = new TestTraceListener ( ) ;
436+ Trace . Listeners . Add ( traceListener ) ;
437+ var db = DatabaseHelper . Open ( ) ;
438+
439+ var q = db . Employees . Query ( ) . LeftJoin ( db . Employees . As ( "Manager" ) , Id : db . Employees . ManagerId ) ;
440+ q = q . Select ( db . Employees . Name , q . Manager . Name . As ( "Manager" ) ) ;
441+ List < dynamic > employees = q . OrderBy ( q . Manager . Name ) . ToList ( ) ;
442+ Trace . Listeners . Remove ( traceListener ) ;
443+ Assert . Greater ( traceListener . Output . IndexOf ( "order by [manager].[name]" , StringComparison . OrdinalIgnoreCase ) , 0 ) ;
444+ }
430445
431446 [ Test ]
432447 public void CanFetchMoreThanOneHundredRows ( )
You can’t perform that action at this time.
0 commit comments