@@ -204,8 +204,14 @@ protected virtual SelectMode GetSelectMode(string path)
204
204
return SelectMode . Undefined ;
205
205
}
206
206
207
+ /// <summary>
208
+ /// Returns null if sorting is not required, otherwise list of indexes in sorted order
209
+ /// </summary>
207
210
private static int [ ] GetTopologicalSortOrder ( List < DependentAlias > fields )
208
211
{
212
+ if ( ! fields . Exists ( a => a . DependsOn ? . Length > 0 ) )
213
+ return null ;
214
+
209
215
TopologicalSorter g = new TopologicalSorter ( fields . Count ) ;
210
216
Dictionary < string , int > indexes = new Dictionary < string , int > ( StringComparer . OrdinalIgnoreCase ) ;
211
217
@@ -799,16 +805,9 @@ protected SqlString MergeOrderings(string ass, string orderBy) {
799
805
/// </summary>
800
806
protected JoinFragment MergeOuterJoins ( IList < OuterJoinableAssociation > associations )
801
807
{
802
- IList < OuterJoinableAssociation > sortedAssociations = new List < OuterJoinableAssociation > ( ) ;
803
-
804
- var indices = GetTopologicalSortOrder ( _dependentAliases ) ;
805
- for ( int index = indices . Length - 1 ; index >= 0 ; index -- )
806
- {
807
- sortedAssociations . Add ( associations [ indices [ index ] ] ) ;
808
- }
809
-
810
808
JoinFragment outerjoin = Dialect . CreateOuterJoinFragment ( ) ;
811
809
810
+ var sortedAssociations = GetSortedAssociations ( associations ) ;
812
811
OuterJoinableAssociation last = null ;
813
812
foreach ( OuterJoinableAssociation oj in sortedAssociations )
814
813
{
@@ -840,6 +839,21 @@ protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associati
840
839
return outerjoin ;
841
840
}
842
841
842
+ private IList < OuterJoinableAssociation > GetSortedAssociations ( IList < OuterJoinableAssociation > associations )
843
+ {
844
+ var indexes = GetTopologicalSortOrder ( _dependentAliases ) ;
845
+ if ( indexes == null )
846
+ return associations ;
847
+
848
+ var sortedAssociations = new List < OuterJoinableAssociation > ( associations . Count ) ;
849
+ for ( int index = indexes . Length - 1 ; index >= 0 ; index -- )
850
+ {
851
+ sortedAssociations . Add ( associations [ indexes [ index ] ] ) ;
852
+ }
853
+
854
+ return sortedAssociations ;
855
+ }
856
+
843
857
/// <summary>
844
858
/// Count the number of instances of IJoinable which are actually
845
859
/// also instances of ILoadable, or are one-to-many associations
0 commit comments