Category Archives: Linq

Using LINQ to randomise a list.

This afternoon I needed to select some random items from a collection, and my searching ran into this idea.

Random rnd = new Random();
var randomizedList = from item in list
                     orderby rnd.Next()
                     select item;

I modified it a little to do what I needed (Select 4 items at random from a list), and this is what I came up with…

Random rnd = new Random();
var MyCollection = (from icon in TheOriginalList orderby rnd.Next() select icon).Take(4));