0

I was wondering about the best way to select random rows from a table using LINQ 2 SQL, while I was searching I found those two questions:

Random row from Linq to Sql

Get a random row with LINQtoSQL

but I don't think that this is the best & fastest way, according to msdn using NewID() is a bad way to select rows randomly and on this article :

http://msdn.microsoft.com/en-us/library/cc441928.aspx

they proposed another Query which is faster than NewID method:

  SELECT * FROM Table1
  WHERE (ABS(CAST(
  (BINARY_CHECKSUM(*) *
  RAND()) as int)) % 100) < 10

the basic idea behind this query is that they want to generate a random number between 0 and 99 for each row in the table, and then choose all of those rows whose random number is less than the value of the specified percent

but when I tried to do this using LINQ I couldn't find an equivalent to BINARY_CHECKSUM

so is their an equivalent for BINARY_CHECKSUM in LINQ ?

and is it really the fastest way to select rows randomly? (if you can help me to convert it to LINQ code it will be much more easier for me)

thanks for your help

Community
  • 1
  • 1

1 Answers1

-1

Something like that would work?

private static readonly Random _randomInstance = new Random();
private readonly Func<int, bool> ChanceSuccess = new Func<float, bool>(f =>
    _randomInstance.Next(0, 101) <= f);

var result = from row in Table1
             where ChanceSuccess(10)
             select row;
AgentFire
  • 8,944
  • 8
  • 43
  • 90