Caveman's Blog

My commitment to learning.

Linq: OrderByDescending operator

leave a comment »


OrderByDescending is one of the query operators in linq and can be can be to sort data, objects by descending order. Following is an example of an employee collection that is sorted in descending order using the OrderByDescending operator. The following code snippet illustrated the ordering of the Employee objects in descending order:

 class Program
    {
        static void Main(string[] args)
        {
            List<Employee> EmployeeList = new List<Employee>();
            Employee employee = new Employee();
            employee.ID = 1001;
            employee.name = "User 1001";
            EmployeeList.Add(employee);

            employee = new Employee();
            employee.ID = 1007;
            employee.name = "User 1007";
            EmployeeList.Add(employee);

            employee = new Employee();
            employee.ID = 1002;
            employee.name = "User 1002";
            EmployeeList.Add(employee);

            var sortedEmployeed = EmployeeList.OrderByDescending(i => i.ID);
            foreach (var emp in sortedEmployeed)
                Console.WriteLine("EmployeeID: {0}", emp.ID);

        }
    }

    public class Employee
    {
        public int ID { get; set; }
        public string name { get; set; }
    }

Advertisements

Written by cavemansblog

January 8, 2012 at 1:38 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: