Getting random records from a table using LINQ to SQL

I needed to get a number of random records from a table. This is a very quick and easy mechanism to use and will work on any table. To load a specific number of random records change the .First() function

DataContext db = new QuoteDataContext();
var quote = db.Quote.OrderBy(q => db.GetNewId()).First();

to be .Take(x)

DataContext db = new QuoteDataContext();
var quote = db.Quote.OrderBy(q => db.GetNewId()).Take(x);

replacing x with however many records you need.

If you got benefit from this article, feel free to:

ABOUT ME

I’m the Managing Director of an IT Company in Adelaide, South Australia. In my day job I work on both Windows and Linux web hosting technologies and Windows and Web .NET development. In my spare time I create tutorials focused on Davinci Resolve, drone’s, and video production, You can find me on YoutubeTwitter,  or Facebook 

Share

Facebook
Twitter
LinkedIn

Related

Simon Holman Tech
Scroll to Top