I have a database with x amount users and I want to randomly get all the users and then write like 50 users out on my site.
Right now I'm only using .take(50)
and retrieves the latest 50 users. I want it to shuffle 50 random from whole table, Any ideas?
This is what my code looks like now:
userList = userList.OrderBy(user => -user.ID).Take(userCount).ToList();
NOTE: userlist
is my list of all users. and as you can see I'm at the moment using lambda with a variable called userCount where I say how many users to list out!
Try this
Random rnd = new Random();
userList = userList.OrderBy(user => rnd.Next()).Take(usercount).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With