Random order with Entity Framework

5

I would like to translate the following SQL statement to a lambda expression:

SELECT TOP 1 * FROM tbPessoa order by NEWID()

What do I put in the OrderBy that will be accepted?

var pessoa = contexto.tbPessoa.OrderBy(???).FirstOrDefault()

Note: the table has more than 3000 records, so ideally this should not be done in memory.

    
asked by anonymous 04.01.2017 / 14:34

1 answer

5

Use Guid.NewGuid() :

var pessoa = contexto.tbPessoa.OrderBy(a => Guid.NewGuid()).FirstOrDefault()
    
04.01.2017 / 14:40