Written By:- Isha Malhotra
In linq if we have requirement to show data randomly then we can use Random class with linq to perfume this task.
To explain I have used following code. First of all take class
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
Now I have created list to add data
List<Employee> emp = new List<Employee>()
{
new Employee(){ id=101, name="isha", age=21},
new Employee(){ id=102, name="neha", age=22},
new Employee(){ id=103, name="pooja", age=24},
new Employee(){ id=104, name="rahul", age=26},
new Employee(){ id=105, name="neelam", age=27},
new Employee(){ id=106, name="avinash", age=28},
new Employee(){ id=107, name="meenakshi", age=29}
};
Now create object of random class and pass it to the orderby extension method in linq
var ran = new Random();
IEnumerable<Employee> result = emp.OrderBy(x => ran.Next()).Take(3);
GridView1.DataSource = result;
GridView1.DataBind();
Now execute the code and you will get new result on every page refresh.
Figure 1
Figure 2
For any query you can send mail at info@techaltum.com
Thanks