Search code examples
servicestack

OrderBy expression with AutoQuery?


Is it possible to create an AutoQuery with a SQL expression for OrderBy?

public class FindStatisticalData : QueryDb<StatisticalData>
{ }   

public IAutoQueryDb AutoQuery { get; set; }
var query = new FindStatisticalData();
query.OrderBy = "iif(report_period=0,0,iif(report_period=1,1,iif(report_period=2,3,iif(report_period=3,2,report_period))))";
var q = AutoQuery.CreateQuery(query, Request, db);

// EXCEPTION thrown:  Could not find field (iif(report_period=0

Solution

  • Only fields can be ordered in the OrderBy on the AutoQuery Request DTO.

    You can try ordering by an expression in the populated SqlExpression<T> using UnsafeOrderBy(), e.g:

    var q = AutoQuery.CreateQuery(query, Request, db);
    q.UnsafeOrderBy(...);