Answered by:
I Need To Display fetch data in random in my Table

Question
-
User-1722502126 posted
Hi
In my Mock_Test Table Number of Questions is There I need to select Question Randomly...
I try to fetch DO like this
using (Models.EOTAEntities dbModel = new Models.EOTAEntities())
{
if (Pssmrk.ToLower() == "nok")
{
if (Qnumber > 0)
{
var qry = from row in dbModel.Mock_Tests
where row.IsMocktest == true
select row;int count = qry.Count(); // 1st round-trip
int index = new Random().Next(count);Mock_Test cust = qry.Skip(index).FirstOrDefault();
//Guid r = new Guid();
//var cust = dbModel.Mock_Tests.Where(h => h.IsMocktest == true).OrderBy(h => r.()).Take(1);
return Ok(cust);}
}
But It's Not working Please any one Help me
Wednesday, June 19, 2019 7:52 PM
Answers
-
User753101303 posted
Do you mean that you have an error? I got a message telling that skip requires ordering. If showing code that fails, pleasealways just tell which error you have and on which line rather than just that "it is not working".
The following seems to work fine (I just added the ordering) :
using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; namespace ConsoleDemo { class Data { [Key] public string Name { get; set; } } class Db:DbContext { public DbSet<Data> Items { get; set; } } class Program { static void Main(string[] args) { using(var db=new Db()) { if (db.Items.Count()==0) { db.Items.Add(new Data { Name = "A" }); db.Items.Add(new Data { Name = "B" }); db.Items.Add(new Data { Name = "C" }); db.Items.Add(new Data { Name = "D" }); db.SaveChanges(); } var qry = from row in db.Items orderby row.Name select row; int count = qry.Count(); // 1st round-trip int index = new Random().Next(count); var cust = qry.Skip(index).FirstOrDefault(); Console.WriteLine(cust.Name); } } } }
I tried also the Guid based variation which seems to work fine as well :
using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; namespace ConsoleDemo { class Data { [Key] public string Name { get; set; } } class Db:DbContext { public DbSet<Data> Items { get; set; } } class Program { static void Main(string[] args) { using(var db=new Db()) { if (db.Items.Count()==0) { db.Items.Add(new Data { Name = "A" }); db.Items.Add(new Data { Name = "B" }); db.Items.Add(new Data { Name = "C" }); db.Items.Add(new Data { Name = "D" }); db.SaveChanges(); } var qry = from row in db.Items orderby Guid.NewGuid() select row; //int count = qry.Count(); // 1st round-trip //int index = new Random().Next(count); var cust = qry.First(); Console.WriteLine(cust.Name); } } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 19, 2019 9:57 PM
All replies
-
User753101303 posted
Hi,
You tried ti see what happens ? index is different each time? Don't use new Random() if calling this multiple times quickly (it would take the first value of a random sequence seeded the same way rather than the next value from a single sequence).
A known approach is to sort on a guid value but you need to create a new value for each row (here your commented line is sorting on the same value for all rows ). For example :
var cust = dbModel.Mock_Tests.Where(h => h.IsMocktest == true).OrderBy(h => Guid.NewGuid())).Take(1); // Linq may not translate that correctly dependiing on your db
Wednesday, June 19, 2019 8:56 PM -
User-1722502126 posted
My execution terminated this line
Mock_Test cust = qry.Skip(index).FirstOrDefault();
I can't recognize...
And each time index is different
And comment line code is not working 1st time i try to like that bit that code is not working then only i comment and change code like thatWednesday, June 19, 2019 9:18 PM -
User753101303 posted
Do you mean that you have an error? I got a message telling that skip requires ordering. If showing code that fails, pleasealways just tell which error you have and on which line rather than just that "it is not working".
The following seems to work fine (I just added the ordering) :
using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; namespace ConsoleDemo { class Data { [Key] public string Name { get; set; } } class Db:DbContext { public DbSet<Data> Items { get; set; } } class Program { static void Main(string[] args) { using(var db=new Db()) { if (db.Items.Count()==0) { db.Items.Add(new Data { Name = "A" }); db.Items.Add(new Data { Name = "B" }); db.Items.Add(new Data { Name = "C" }); db.Items.Add(new Data { Name = "D" }); db.SaveChanges(); } var qry = from row in db.Items orderby row.Name select row; int count = qry.Count(); // 1st round-trip int index = new Random().Next(count); var cust = qry.Skip(index).FirstOrDefault(); Console.WriteLine(cust.Name); } } } }
I tried also the Guid based variation which seems to work fine as well :
using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; namespace ConsoleDemo { class Data { [Key] public string Name { get; set; } } class Db:DbContext { public DbSet<Data> Items { get; set; } } class Program { static void Main(string[] args) { using(var db=new Db()) { if (db.Items.Count()==0) { db.Items.Add(new Data { Name = "A" }); db.Items.Add(new Data { Name = "B" }); db.Items.Add(new Data { Name = "C" }); db.Items.Add(new Data { Name = "D" }); db.SaveChanges(); } var qry = from row in db.Items orderby Guid.NewGuid() select row; //int count = qry.Count(); // 1st round-trip //int index = new Random().Next(count); var cust = qry.First(); Console.WriteLine(cust.Name); } } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 19, 2019 9:57 PM