Random sort list with LINQ

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:
public class Employee
{
    public int Id
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }
}
This is how you can randomly sort the List object:
List list = new List();



list.Add(new Employee { Id = 1, Name = "Luis" });
list.Add(new Employee { Id = 2, Name = "Vieira" });
list.Add(new Employee { Id = 3, Name = "Joana" });
list.Add(new Employee { Id = 4, Name = "Laura" });
list.Add(new Employee { Id = 5, Name = "Antonio" });
list.Add(new Employee { Id = 6, Name = "Toino" });
list.Add(new Employee { Id = 7, Name = "Lara" });
list.Add(new Employee { Id = 8, Name = "Filipe" });
list.Add(new Employee { Id = 9, Name = "Miguel" });

list = list.OrderBy(emp => Guid.NewGuid()).ToList();




From: http://www.dailycoding.com/Posts/random_sort_a_list_using_linq.aspx

0 comentários:

Enviar um comentário