LINQ to SQL - Select N Random Records

access_time 2 months ago visibility32 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 at 2 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

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.

Learn more arrow_forward

More from Kontext

local_offer .NET local_offer C#

visibility 9
thumb_up 0
access_time 2 months ago

In many solutions, .NET standard has been used to share code between .NET Framework and .NET Core projects. Since the release of .NET 5, you probably will think what is the .NET standard version for C# 9 language features. Well, the answer is simple - there is no need to have another version of ...

local_offer .NET local_offer spark local_offer parquet local_offer hive local_offer dotnetcore

visibility 1742
thumb_up 0
access_time 2 years ago

I’ve been following Mobius project for a while and have been waiting for this day. .NET for Apache Spark v0.1.0 was just published on 2019-04-25 on GitHub. It provides high performance APIs for programming Apache Spark applications with C# and F#. It is .NET Standard complaint and can run in ...

local_offer teradata local_offer C# local_offer .NET

visibility 7993
thumb_up 0
access_time 6 years ago

In this post, I will demonstrate how to connect to Teradata database via .NET Data Provider for Teradata using C#. Install the .NET Data Provider for Teradata from the following link: Teradata Tools and Utilities Windows Installation Package Name it as’' ‘ConsoleApps.Teradata’. Add ...

About column

Everything about .NET framework, .NET Core and .NET Standard. 

rss_feed Subscribe RSS