Is there a way to return a random row from a table using LINQToSQL?
Never tried it, but i suspect the following will get the job done
Random rnd = new Random();
int count = db.MyTable.Count();
int skip = rnd.Next(count);
var row = (from r in db.MyTable
select r).Skip(skip).Take(1);
Yes, generate a random number in the range of the table's count and Skip() that number, Take(1) then return the first element of the resulting sequence. Something like
var R = new Random();
var ToSkip = R.Next(0, ctx.Customers.Count);
return ctx.Customers.Skip(ToSkip).Take(1).First();
But it's a bit ugly. Is there an actual requirement that calls for getting a random row in a SQL table?
You can use a random number and Skip()
to get to that row:
int randomNum = 10; // <-- determined by your favorite RNG
var row = mydb.Objects.Skip(randomNum).Take(1);
You can get LinqToSQL to generate SQL which uses the SQL Server NEWID() function. Here is my implementation:
namespace Data // change to whatever the namespace of your DataContext is, or remove
{
/// <summary>
/// Add RANDOM() extension to the Data context...
/// </summary>
partial class DefaultDataContext // change to the name of your DataContext
{
[System.Data.Linq.Mapping.Function(Name = "NEWID", IsComposable = true)]
public Guid Random()
{
// this code is not actually executed, it simply provides a way to access
// T-SQL "NEWID()" function from Linq to SQL
throw new NotImplementedException();
}
}
}
Example of usage, to pull a random product from the database:
var product = (from p in db.Products // our DataContext instance is called db here
orderby db.Random()
select p).FirstOrDefault()
technically:
var randomRow = (from rows in db.Table
select shipper).First();
because you can't ever be sure of the row order, unless you specify the order by clause.
I'd try something like:
query.Skip(rand.Next()).Take(1).FirstOrDefault();
With rand
being initialised as returning a number between a specific range.
Be sure to check for null as the return value in case the random number generated was greater than the number of rows (of course you could initialise it's upper bound as the number of rows, too).