Thursday, February 9, 2012

Order By Class List<> in C#

Order by Ascending and Descending class List<> in C# :

Introduction : In this article i will explain you how to sort class collection ( List<> of class ) in linq c# . I done the easy code to understand how to uses Order by Ascending and Descending of class list in linq c#.

Class example for sorting class List<> In c#: Following is the user class example with constructor in c#.
public class User
{
    public int UserId { get; set; }
    public string Firstname { get; set; }
    public User(int userId, string firstname)
    {
        UserId = userId;
        Firstname = firstname;
    }
}
Ascending and Descending Sorting class list in linq C#:
List<User> UserList = new List<User>(); 

UserList.Add(new User(1, "bimal"));
UserList.Add(new User(2, "punit"));
UserList.Add(new User(3, "amit"));

UserList = UserList.OrderBy(x => x.Firstname).ToList();  //Ascending Sorted the class list using linq.
UserList = UserList.OrderByDescending(x => x.Firstname).ToList();  // Descending Sorted the class list using linq.


Related Other posts

0 comments:

Post a Comment