Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to entities Query to randomize row selection

I am writing an app using .NET, C#, LINQ to entities and SQL Server 2008.

I would like to pick a row randomly from a table. Is there a way to achieve this using the LINQ queries. One approach would be to get a list of rows from the table and then pick one of them randomly, which is very straight forward.

Just curious, if there is a way to include the randomness attribute in the LINQ.

like image 499
pencilslate Avatar asked Dec 29 '22 17:12

pencilslate


1 Answers

Excuse the Pseudocode:

    static IEnumerable<RowType> RandomRows()
    {
        while (true)
        {
            yield return GetRowByID((new Random).Next(NumberOfRowsInTable));
        }
    }
like image 85
Robert Venables Avatar answered Jan 01 '23 17:01

Robert Venables