@@ -116,6 +116,13 @@ public IHttpActionResult GetAssetsPaged([FromBody] PostData postData, [FromUri]
116116 PagedResults results = new PagedResults ( ) ;
117117 results . RecordsPerPage = recordsPerPage ;
118118
119+ string orderBy = "ID" ;
120+
121+ if ( typeof ( Asset ) . GetProperties ( ) . Select ( prop => prop . Name ) . Contains ( postData . OrderBy ) || postData . OrderBy == "AssetType" )
122+ {
123+ orderBy = postData . OrderBy ;
124+ }
125+
119126 using ( AdoDataConnection connection = new AdoDataConnection ( Connection ) )
120127 {
121128 string sql = $@ "SELECT
@@ -144,7 +151,7 @@ GROUP BY
144151 AssetType.Name,
145152 AssetAssetGroup.AssetGroupID
146153 HAVING AssetAssetGroup.AssetGroupID = {{0}}
147- ORDER BY { postData . OrderBy } { ( postData . Ascending ? "ASC" : "DESC" ) }
154+ ORDER BY { orderBy } { ( postData . Ascending ? "ASC" : "DESC" ) }
148155 " ;
149156
150157 DataTable records = connection . RetrieveData ( sql , assetGroupID ) ;
@@ -280,6 +287,11 @@ public IHttpActionResult GetMetersPaged([FromBody] PostData postData, [FromUri]
280287 PagedResults results = new PagedResults ( ) ;
281288 results . RecordsPerPage = recordsPerPage ;
282289
290+ string orderBy = "ID" ;
291+
292+ if ( typeof ( Meter ) . GetProperties ( ) . Select ( prop => prop . Name ) . Contains ( postData . OrderBy ) )
293+ orderBy = postData . OrderBy ;
294+
283295 using ( AdoDataConnection connection = new AdoDataConnection ( Connection ) )
284296 {
285297 string sql = $@ "SELECT DISTINCT
@@ -306,7 +318,7 @@ GROUP BY
306318 Location.Name,
307319 MeterAssetGroup.AssetGroupID
308320 HAVING MeterAssetGroup.AssetGroupID = {{0}}
309- ORDER BY { postData . OrderBy } { ( postData . Ascending ? "ASC" : "DESC" ) }
321+ ORDER BY { orderBy } { ( postData . Ascending ? "ASC" : "DESC" ) }
310322 " ;
311323
312324 DataTable records = connection . RetrieveData ( sql , assetGroupID ) ;
@@ -446,9 +458,9 @@ public IHttpActionResult GetSubGroupsPaged([FromBody] PostData postData, [FromUr
446458 {
447459 IEnumerable < AssetGroupView > records = new TableOperations < AssetGroupView > ( connection ) . QueryRecordsWhere ( "ID in (SELECT ChildAssetGroupID FROM AssetGroupAssetGroupView WHERE ParentAssetGroupID = {0})" , assetGroupID ) ;
448460 if ( postData . Ascending )
449- records = records . OrderBy ( record => record . GetType ( ) . GetProperty ( postData . OrderBy ) . GetValue ( record ) ) ;
461+ records = records . OrderBy ( record => record . GetType ( ) . GetProperty ( postData . OrderBy ) . GetValue ( record ) ?? record . ID ) ;
450462 else
451- records = records . OrderByDescending ( record => record . GetType ( ) . GetProperty ( postData . OrderBy ) . GetValue ( record ) ) ;
463+ records = records . OrderByDescending ( record => record . GetType ( ) . GetProperty ( postData . OrderBy ) . GetValue ( record ) ?? record . ID ) ;
452464
453465 results . TotalRecords = records . Count ( ) ;
454466 results . NumberOfPages = ( records . Count ( ) + recordsPerPage - 1 ) / recordsPerPage ;
0 commit comments