Search code examples
sql-serversql-order-byservicestackormlite-servicestack

Problems with OrmLite query OrderBy method


I am having a few related problems with the OrderBy method when generating a query for OrmLite. The following two statements work:

.OrderBy(ob => new { at = ob.SortBy, ob.Id });
.OrderBy(ob => new { at = Sql.Desc(ob.SortBy), ob.Id });

But the following statement gives me a compile error (Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access):

.OrderBy(ob => new { at = Sql.Desc(ob.SortBy), Sql.Desc(ob.Id) });

And the following statement gives me a sql error (Incorrect syntax near the keyword 'ASC'):

.OrderBy(ob => new { at = Sql.Desc(ob.SortBy) });

And when digging deeper I see that the OrderByExpression gives me:

ORDER BY "SortBy" DESC ASC


Solution

  • So... the semi-obvious solution/workaround now that I've been working with OrmLite for a few days is to just concatenate statements and also using OrderByDescending... for example

    .OrderByDescending(ob => ob.SortBy).OrderByDescending(ob => ob.Id)
    

    or

    .OrderByDescending(ob => ob.SortBy)