LINQ to SQL - Select N Random Records

access_time 4 months ago visibility147 comment 0

Language-Integrated Query (LINQ) is a set of technologies based on the integration of query capabilities directly into the C# or VB language in .NET. It allows intuitive query against SQL databases, XML, object list, etc. 

This article shows how to return a top N records randomly. 


The following is one example of querying a table in a database via EntityFramework:

var query = from t in entityStore.Entities
	    select t;
var recordCount = 10;
var entityList = await query.AsNoTracking().Take(recordCount).ToArrayAsync().ConfigureAwait(false);

The result will show the top 10 records from the backend table of the entity store.

Random results

To return random results, we can simply add an OrderBy step to the integrated query and the value needs to be random.

For example, the following code snippet simply uses Guid.NewGuid() to return a random value for each record. 

var query = from t in entityStore.Entities
	    select t.Content;
var recordCount = 10;
var entityList = await query.OrderBy(t => Guid.NewGuid()).AsNoTracking().Take(recordCount).ToArrayAsync().ConfigureAwait(false);

In SQL Server, the above code will generate a SELECT statement with a new column derive using T-SQL function NEWID()

For databases that don't support unique GUID data type, the entire records may need to be brought back into memory to perform OrderBy action, which may lead to bad performance. Please take this into consideration when using this approach.

info Last modified by Raymond 4 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 1470
thumb_up 0
access_time 6 years ago

In my previous post, I demonstrated how to use ADO.NET to connect to Teradata in your OLTP projects.  In this sample, I am going to implement the same sample using ADO.NET Entity Model, Entity Framework and LINQ. Connect to Teradata Database via .NET Data Provider Entity Framework ...

visibility 2398
thumb_up 0
access_time 4 years ago

Migrating from ASP.NET Core 1.x to 2.0 is not an easy job especially if you have customized Identity and used customized authentication. This post summarizes the issues and errors I have experienced and their resolutions when upgrading my project. Hopefully it can save you sometime if you are doing ...

Modern Web Application - Azure Blob Storage for Uploaded Files
visibility 2503
thumb_up 0
access_time 10 months ago

With cloud platforms like Azure, we can totally separate user content storage from web application storage to decouple components from each other and to make the application easy to scale and deploy. This article provides detailed information with code snippets about how to use Azure server-less product Blob Storage and App Service to enable horizontally scalable web application for users to upload files (BLOBs).