How to shuffle list in c#
For ordering a list to specific order we use OrderBy but in some case we need the list to be ordered differently every time we try to access the list. In that case order by becomes useless as we don't want to follow any specific order. So for that purpose we use Random class like this
List<Author> authors = new List<Author> { new Author { Name = "Code", Book = "c# Programming", Price = 59.95 }, new Author { Name = "Code2Night", Book = "Shuffle", Price = 69.95 }, new Author { Name = "Blogs", Book = "Lists", Price = 79.95 } }; var rnd = new Random(); authors = authors.OrderBy(item => rnd.Next()).ToList();
So, here we will use Random() for generating random order every time and which will shuffle the list every time you will try to access the data. This is how to shuffe list in c#.