Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:17:44+00:00 2026-05-11T10:17:44+00:00

What is the best (and fastest) way to retrieve a random row using Linq

  • 0

What is the best (and fastest) way to retrieve a random row using Linq to SQL when I have a condition, e.g. some field must be true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:17:44+00:00Added an answer on May 11, 2026 at 10:17 am

    You can do this at the database, by using a fake UDF; in a partial class, add a method to the data context:

    partial class MyDataContext {      [Function(Name='NEWID', IsComposable=true)]       public Guid Random()       { // to prove not used by our C# code...           throw new NotImplementedException();       } } 

    Then just order by ctx.Random(); this will do a random ordering at the SQL-Server courtesy of NEWID(). i.e.

    var cust = (from row in ctx.Customers            where row.IsActive // your filter            orderby ctx.Random()            select row).FirstOrDefault(); 

    Note that this is only suitable for small-to-mid-size tables; for huge tables, it will have a performance impact at the server, and it will be more efficient to find the number of rows (Count), then pick one at random (Skip/First).


    for count approach:

    var qry = from row in ctx.Customers           where row.IsActive           select row;  int count = qry.Count(); // 1st round-trip int index = new Random().Next(count);  Customer cust = qry.Skip(index).FirstOrDefault(); // 2nd round-trip 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best (fastest) way to check if a linq object is referenced
What is the best/fastest way to merge two xml documents with ruby? I have
I need to know the best (fastest) way to have a server (preferably a
Please, whats the best way for checking if some text sentence have some of
Which is the best and fastest way to find the element using webdriver? By.XPath
How is the Best and fastest way to do it?
What's the fastest/best way to compare two arrays and return the difference? Much like
I am interested to find out what is the best/fastest (most efficient) way to
What is the best, easiest, and fastest way to use a template for a
I would like to know what is the best, fastest and easiest way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.