LINQ (Language Integrated Query) is a one of very useful feature of .net 3.5 framework. This allows you to query objects and perform complex operations simply and efficiently. There are lots of trick which can be used with the help of LINQ. In this article I am explaining how can we random sort a List using LINQ
Random Sort
Consider the below Employee class:
This is how you can randomly sort the List<Employee> object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Employee { public int Id { get; set; } public string Name { get; set; } } |
This is how you can randomly sort the List<Employee> object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | List<Employee> list = new List<Employee>(); list.Add(new Employee { Id = 1, Name = "Davolio Nancy" }); list.Add(new Employee { Id = 2, Name = "Fuller Andrew" }); list.Add(new Employee { Id = 3, Name = "Leverling Janet" }); list.Add(new Employee { Id = 4, Name = "Peacock Margaret" }); list.Add(new Employee { Id = 5, Name = "Buchanan Steven" }); list.Add(new Employee { Id = 6, Name = "Suyama Michael" }); list.Add(new Employee { Id = 7, Name = "King Robert" }); list.Add(new Employee { Id = 8, Name = "Callahan Laura" }); list.Add(new Employee { Id = 9, Name = "Dodsworth Anne" }); list = list.OrderBy(emp => Guid.NewGuid()).ToList(); |