views:

473

answers:

2

As the title says, what's the most efficient way to get a random selection of x DataRows from a DataTable.

Would it be to iteratively do something like the following until I have as many as I need?

protected DataRow SelectRandomRow(DataTable dataTable, Random randomSelector)
{
    return dataTable.Rows[randomSelector.Next(dataTable.Rows.Count)];
}

There must be a better way..?

+1  A: 

Your solution seems like a perfectly reasonable option.

You'll probably want to add a check for duplicate rows being returned, however, as it could return the same row multiple times.

Reed Copsey
+1  A: 

For a random drug testing system I was working on, I actually went one step back to the query of qualified records and added a random() column as a column in the result set, and ordered by that column.

select randomFunction() as RandSequence, otherfields from table order by 1

Since a random is based on an original seed, and it not going to be run at the exact same time, same second, based on probability and human interjection, it worked great. I ran the query like 50,000 times against the same "pool" of people -- one such group had over 800 people, and I stored the results. At any given time, a person was only ever in the exact same position only about 4 times, but based totally on the decimal precision difference.

Then, you can always take the top X records directly from your result set.

DRapp