Search code examples
c#coolstoragevici

How do I order by NEWID in CoolStorage?


How can I order my resultset by NEWID to get a random order?


Solution

  • Out of the box, this is not possible, but there's an easy way to embed provider-specific expressions in the OrderedBy() and FilteredBy() expressions.

    In your case (assuming you're using SQL Server):

    Order.List().OrderedBy("$NEWID()");
    

    What happens here is that every word with a "$" prefix is sent to the database provider "as is" (without the "$" of course). The disadvantage is that this will only work for one provider (so you can't simply switch to MySql and make it work without changing code)