1

i have a query which will result 10 or 20 or any number number of rows. Below is the query.

 var q = (from c in session.DB.Question where c.Level='1' && c.Group='1' select c); 

This query can give me any number of rows.

But i have to show just 1 row from the result. I can select top 1/first but i would like select randomly.

i saw a topic about this: How to request a random row in SQL? but i want it in LinQ

Please help me how to get random row from the result.

3
  • 3
    Technically, First() without a sort will get you an 'random' row ;) Commented Feb 11, 2016 at 3:28
  • Possible duplicate of EF Code First: How to get random rows Commented Feb 11, 2016 at 3:28
  • Do you have any identity keys from your questionnaires? Commented Feb 11, 2016 at 3:40

4 Answers 4

3

Sort items by random value and select first:

var q = (from c in session.DB.Question where c.Level =='1' && c.Group =='1' select c) .OrderBy(o => SqlFunctions.Rand()) .First(); 
Sign up to request clarification or add additional context in comments.

8 Comments

thanks, whats the namespacec for sqlFunction.Rand() ?
If you've added the reference, then System.Data.Objects.SqlClient. msdn.microsoft.com/en-us/library/dd487168(v=vs.110).aspx
@AlexR. em, it's the same link i posted in answer
i've added "using system.data.sqlcliet" but still sqlFunctions.Rand() is unknown.
@Backs "the name 'sqlfunctions ' does not exist in the current context "
|
1

Thanks all. i found it and working:

 var q = (from c in Session.DB.WinQuestionSet where c.Level == "easy" && c.Grade == "1" select c).OrderBy(x => Guid.NewGuid()).Take(1).Single(); label1.Text = q.Text; 

Comments

0

I assume that your questions has an Id. Please see example below. I used Random class to generate random number.

 List<Question> questions = new List<Question> { new Question { Id = 10, Name = "What?" }, new Question { Id = 12, Name = "How?" }, new Question { Id = 32, Name = "When?" }, new Question { Id = 41, Name = "Where?" }, }; var q = (from c in questions select c); int i = 1; Dictionary<int, int> questionKeys = new Dictionary<int, int>(); foreach (var item in questions) { questionKeys.Add(i, item.Id); i++; } Random rdm = new Random(); int randomRow = rdm.Next(1, q.Count()); var questionId = questionKeys.Where(x => x.Key == randomRow).Select(x => x.Value).Single(); var result = q.Where(x => x.Id == questionId).Single(); Console.WriteLine(result.Name); 

6 Comments

tnx, But maybe the numbers are not in sequence, so int randomRow = rdm.Next(1, q.Count()); may not contains ID in result
what if Id is not sequence from 1 to Count?
@Backs yes,exactly
Put your collection or the not sequence id to a dictionary<int, int> and populate the key as sequence then use the random number
as you can see, author need a LinqToSQL solution. so, using dictionaries can be a bad one, when you suddenly get 1mln rows instead of 10-20
|
-1

Assuming data is your data rows:

Random rand = new Random(); var row = data.ElementAt(rand.Next(data.Count)); 

Note that this does not work for Linq to SQL, and thus should be used after you query your db.

2 Comments

LinqToSql does not support ElementAt stackoverflow.com/questions/5147767/…
I did not consider that, good catch. I will update my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.