How do I order by NEWID in CoolStorage?

401 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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)