HI WELCOME TO KANSIRIS

Retrieve Random Records using Entity Framework

Leave a Comment
var data = DB Call to retrieve all records from table; // Retrieve All Records from the Table
Random r = new Random(); // Declare Random Class
int rInt = r.Next(0, data.Count);  // Here it’ll take some integer from 0 to retrieved records count
Eg: int rInt = r.Next(0, 10); //Here rInt will take some random number in 0 to 10 range
ViewBag.getdata = data.Skip(rInt).Take(3).ToList(); // Mention randomnly generated number or integer inside Skip, then entity framework will skip to that particular records and takes next 3 records.
Output:
  1. Refresh Page everytime to see randomly retrieved records.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.