Thursday, December 15, 2011

Shuffle in Linq (Part 1)

A simple implementation for shuffling a list in linq.
public static class ShuffleExtensions
{
    public static IEnumerable<tsource> 
           RandomShuffle<tsource>(this IEnumerable<tsource> source)
    {
        var rand = new Random();
        return source.Select(t => new { 
                Index = rand.Next(), 
                Value = t })
            .OrderBy(p => p.Index)
            .Select(p => p.Value);
    }
}

Submit this story to DotNetKicks