@@ -272,6 +272,83 @@ public virtual SqlExpression<T> OrderBy(string orderBy)
272272 return this ;
273273 }
274274
275+ public ModelDefinition GetModelDefinition ( FieldDefinition fieldDef )
276+ {
277+ if ( modelDef . FieldDefinitions . Any ( x => x == fieldDef ) )
278+ return modelDef ;
279+
280+ return tableDefs
281+ . FirstOrDefault ( tableDef => tableDef . FieldDefinitions . Any ( x => x == fieldDef ) ) ;
282+ }
283+
284+ private SqlExpression < T > OrderByFields ( string orderBySuffix , FieldDefinition [ ] fields )
285+ {
286+ orderByProperties . Clear ( ) ;
287+
288+ var sbOrderBy = new StringBuilder ( ) ;
289+ foreach ( var field in fields )
290+ {
291+ var tableDef = GetModelDefinition ( field ) ;
292+ var qualifiedName = modelDef != null
293+ ? DialectProvider . GetQuotedColumnName ( tableDef , field )
294+ : DialectProvider . GetQuotedColumnName ( field ) ;
295+
296+ if ( sbOrderBy . Length > 0 )
297+ sbOrderBy . Append ( ", " ) ;
298+
299+ sbOrderBy . Append ( qualifiedName + orderBySuffix ) ;
300+ }
301+
302+ this . orderBy = sbOrderBy . Length == 0
303+ ? null
304+ : "ORDER BY " + sbOrderBy ;
305+ return this ;
306+ }
307+
308+ public virtual SqlExpression < T > OrderByFields ( params FieldDefinition [ ] fields )
309+ {
310+ return OrderByFields ( "" , fields ) ;
311+ }
312+
313+ public virtual SqlExpression < T > OrderByFieldsDescending ( params FieldDefinition [ ] fields )
314+ {
315+ return OrderByFields ( " DESC" , fields ) ;
316+ }
317+
318+ private SqlExpression < T > OrderByFields ( string orderBySuffix , string [ ] fieldNames )
319+ {
320+ orderByProperties . Clear ( ) ;
321+
322+ var sbOrderBy = new StringBuilder ( ) ;
323+ foreach ( var fieldName in fieldNames )
324+ {
325+ var field = FirstMatchingField ( fieldName ) ;
326+ if ( field == null )
327+ throw new ArgumentException ( "Could not find field " + fieldName ) ;
328+ var qualifiedName = DialectProvider . GetQuotedColumnName ( field . Item1 , field . Item2 ) ;
329+
330+ if ( sbOrderBy . Length > 0 )
331+ sbOrderBy . Append ( ", " ) ;
332+
333+ sbOrderBy . Append ( qualifiedName + orderBySuffix ) ;
334+ }
335+
336+ this . orderBy = sbOrderBy . Length == 0
337+ ? null
338+ : "ORDER BY " + sbOrderBy ;
339+ return this ;
340+ }
341+
342+ public virtual SqlExpression < T > OrderByFields ( params string [ ] fieldNames )
343+ {
344+ return OrderByFields ( "" , fieldNames ) ;
345+ }
346+
347+ public virtual SqlExpression < T > OrderByFieldsDescending ( params string [ ] fieldNames )
348+ {
349+ return OrderByFields ( " DESC" , fieldNames ) ;
350+ }
351+
275352 public virtual SqlExpression < T > OrderBy < TKey > ( Expression < Func < T , TKey > > keySelector )
276353 {
277354 sep = string . Empty ;
@@ -283,6 +360,14 @@ public virtual SqlExpression<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelec
283360 return this ;
284361 }
285362
363+ public virtual SqlExpression < T > ThenBy ( string orderBy )
364+ {
365+ orderBy . SqlVerifyFragment ( ) ;
366+ orderByProperties . Add ( orderBy + " ASC" ) ;
367+ BuildOrderByClauseInternal ( ) ;
368+ return this ;
369+ }
370+
286371 public virtual SqlExpression < T > ThenBy < TKey > ( Expression < Func < T , TKey > > keySelector )
287372 {
288373 sep = string . Empty ;
@@ -304,6 +389,14 @@ public virtual SqlExpression<T> OrderByDescending<TKey>(Expression<Func<T, TKey>
304389 return this ;
305390 }
306391
392+ public virtual SqlExpression < T > ThenByDescending ( string orderBy )
393+ {
394+ orderBy . SqlVerifyFragment ( ) ;
395+ orderByProperties . Add ( orderBy + " DESC" ) ;
396+ BuildOrderByClauseInternal ( ) ;
397+ return this ;
398+ }
399+
307400 public virtual SqlExpression < T > ThenByDescending < TKey > ( Expression < Func < T , TKey > > keySelector )
308401 {
309402 sep = string . Empty ;
0 commit comments