Getting 3 random entries from a table

I read a few answers to a similar query, but no one seems to hit the mark.

Imagine I have a table containing 10 rows, how can I get 3 random rows from this table using Entity Framework? Not only 1 random line, but 3 random lines - each of them is different from the other?

Thanks in advance

+7
source share
2 answers
var threeRandomFoos = foos.OrderBy(x => Guid.NewGuid()).Take(3); 
+18
source

Instead, there is an easier way:

 var threeRandomFoos = foos.OrderBy( x=> SqlFunctions.Rand()).Take(3); 

Guid.NewGuid will be a little slower in performance, why not use the Random defined by SqlFunctions itself?

+8
source

All Articles