Get X random elements from table in database using Linq or lambda in C#

前端 未结 5 1998
轻奢々
轻奢々 2021-01-11 14:22

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)

5条回答
  •  伪装坚强ぢ
    2021-01-11 15:02

    Try this

    Random rnd = new Random();
    userList = userList.OrderBy(user => rnd.Next()).Take(usercount).ToList();
    

提交回复
热议问题