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
Why not create a generic helper and/or extension?!
namespace My.Core.Extensions
{
public static class EnumerableHelper
{
private static Random r;
static EnumerableHelper()
{
r = new Random();
}
public static T Random(IEnumerable input)
{
return input.ElementAt(r.Next(input.Count()));
}
}
public static class EnumerableExtensions
{
public static T Random(this IEnumerable input)
{
return EnumerableHelper.Random(input);
}
}
}
Usage would be:
var list = new List() { 1, 2, 3, 4, 5 };
var output = list.Random();