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)
Extension Without Random
public static class MyExtensions
{
public static IEnumerable<T> GetRandomItems<T>(this IEnumerable<T> source, Int32 count)
{
return source.OrderBy(s => Guid.NewGuid()).Take(count);
}
}
And now you can
userList = userList.GetRandomItems().ToList();
When it is okay to load all users and then take 50 of them, you can use a while loop like this:
List<object> randomUsers = new List<User>();
Random r = new Random();
while (randomUsers.Count < 50)
{
userList
// skip previously selected users
.Except(randomUsers)
// random number to skip a random amount of users
.Skip(r.Next(0, userList.Count() - 1 - randomUsers.Count))
// and then take the next one
.First();
}
Try this
Random rnd = new Random();
userList = userList.OrderBy(user => rnd.Next()).Take(usercount).ToList();
There are 2 ways of doing this depending on how many users are in your system
1.
List<int> ids = new List<int>(50);
int total = userList.Count();
Random r = new Random();
while (ids.Count() < 50)
{
var next = r.Next(total);
if (!ids.Contains(next))
ids.Add(next);
}
var users = userList.Where(a => ids.Contains(a.ID));
2.
MikeSW beat me to that one
Difference between the options is that 1) involves 2 queries queries to the database and 2) involves loading all of the users from the database just to load 50 of them. It's up to you which is a better way of doing it.
Assuming you have an Id primary key column in your entity I would recommend to use the following query or you will be bringing down a lot of data for no reason.
var rnd = new Random();
// Get the employeeIds.
var employeeIds = EmployeeService.Data()
.Select(e => e.Id)
.ToList();
// Choose n number of random employeeIds.
var randomEmployeeIds = employeeIds
.OrderBy(id => rnd.Next())
.Take(x);
// Get random employees.
var employees = EmployeeService.Data().Where(emp => randomEmployeeIds.Contains(emp.Id));