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:


I’m the Technical Director at Expeed Technology 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 tinker with video production, photograpy, and all things Azure, including IAAS, PAAS and Serverless. You can find me on Twitter over at @simonholman 


Share on facebook
Share on twitter
Share on linkedin



Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top