Saturday, August 8, 2015

Randomly select rows from a datatable in c#

Sometimes we need to show some random rows from a datatable in c#.For achieving this we can use Random Class which is present in base class library and create a random number .Now we use this random number to order datatable.Here is the code snippet for the same.
var rand = new Random(); var employees = employee.AsEnumerable().OrderBy(r => rand.Next());
Further we can select limited number of rows by slightly modifying the above syntax.
var rand = new Random(); var employees = employee.AsEnumerable().OrderBy(r => rand.Next()).Take(10);
Take method will select number of rows specified as parameter

No comments :

Post a Comment