Random order with Entity Framework and MySQL

Viewed 3796

Summary: I want to find a way to make random order with Entity Framework and MySQL (that's important). That solution shouldn't use raw sql queries or ordering AFTER loading all values from database.

What I've tried:

I took idea about using NewGuid() for random order from that answer.

Code:

var query = from e in context.Table
orderby Guid.NewGuid()
select e;
var test = query.FirstOrDefault();

Always thrown exception:

An error occurred while executing the command definition. See the inner exception for details.
Inner exception:
FUNCTION MyDatabase.NewGuid does not exist System.Exception {MySql.Data.MySqlClient.MySqlException}

Seems that problem is that MySQL doesn't have function NewGuid().

How can I order by MySQL function RAND() instead of NewGuid(). In other words, how to use custom function RAND in Entity Framework?

4 Answers
Related