Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Linq Query

Tags:

.net

random

linq

how I can get a random row from a selection in my linq query?

I tried:

Bot bot = (from a in dc.Bot
           select a).OrderBy(x => Guid.NewGuid()).First();

But doesn't work, I ever get the same.

like image 546
PassionateDeveloper Avatar asked Nov 18 '25 21:11

PassionateDeveloper


1 Answers

I would use Skip

var query = from a in dc.Bot
            select a;

int random = new Random().Next(query.Count);

Bot bot = query.Skip(random).First();
like image 125
Bob Avatar answered Nov 21 '25 09:11

Bob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!