Search code examples
mysqlormlite-servicestack

How to order random in ServiceStack OrmLite?


I want to order by "NEWID()" or Guid.NewGuid() but couldn't make it work. I didn't find any examples or documentation about this subject.

Thanks in advance


Solution

  • Since Random ordering is specific to each RDBMS provider you would need to order by a custom SQL Function which you can do using a Custom SqlExpression, e.g. for SQL Server:

    var randomRows = db.Select<Poco>(q => q.OrderBy("NEWID()"));
    

    However I've just added support for the new OrderByRandom() API in this commit which abstracts the way the RDBMS specific API's to retain a portable API that you can now query with:

    var randomRows = db.Select<Poco>(q => q.OrderByRandom());
    

    This change is available from v4.0.43+ that's now available on MyGet.