Search code examples
c#.netlinqrandom

Random element of List<T> from LINQ SQL


I'm using C# 3.5 and am currently using Linq to get all users from a user table and put them in a list.

Now I would like to return a random user from that list. What's the best way to go about doing that?

Edit: Found it here: How to get a Random Object using Linq


Solution

  • Like this:

    var rand = new Random();
    var user = users[rand.Next(users.Count)];