Dynamic LINQ Multi Sorting

  1. Create class LinqDynamicMultiSorting,
    1. public static class LinqDynamicMultiSorting   
    2. {  
    3.     /// <summary>  
    4.     /// 1. The sortExpressions is a list of Tuples, the first item of the  
    5.     /// tuples is the field name,  
    6.     /// the second item of the tuples is the sorting order (asc/desc) case sensitive.  
    7.     /// 2. If the field name (case sensitive) provided for sorting does not exist  
    8.     /// in the object,  
    9.     /// exception is thrown  
    10.     /// 3. If a property name shows up more than once in the "sortExpressions",  
    11.     /// only the first takes effect.  
    12.     /// </summary>  
    13.     public static IEnumerable < T > MultipleSort < T > (this IEnumerable < T > data,  
    14.         List < Model.GridSort > gridsorts)  
    15.     {  
    16.         var sortExpressions = new List < Tuple < string,  
    17.             string >> ();  
    18.         for (int i = 0; i < gridsorts.Count(); i++)  
    19.         {  
    20.             var fieldName = gridsorts[i].Field.Trim();  
    21.             var sortOrder = (gridsorts[i].Dir.Length > 1) ?  
    22.                 gridsorts[i].Dir.Trim().ToLower() : "asc";  
    23.             sortExpressions.Add(new Tuple < stringstring > (fieldName, sortOrder));  
    24.         }  
    25.         // No sorting needed  
    26.         if ((sortExpressions == null) || (sortExpressions.Count <= 0))  
    27.         {  
    28.             return data;  
    29.         }  
    30.         // Let us sort it  
    31.         IEnumerable < T > query = from item in data select item;  
    32.         IOrderedEnumerable < T > orderedQuery = null;  
    33.         for (int i = 0; i < sortExpressions.Count; i++)  
    34.         {  
    35.             // We need to keep the loop index, not sure why it is altered by the Linq.  
    36.             var index = i;  
    37.             Func < T, object > expression = item => item.GetType()  
    38.                 .GetProperty(sortExpressions[index].Item1)  
    39.                 .GetValue(item, null);  
    40.             if (sortExpressions[index].Item2 == "asc")  
    41.             {  
    42.                 orderedQuery = (index == 0) ? query.OrderBy(expression) :  
    43.                     orderedQuery.ThenBy(expression);  
    44.             } else  
    45.             {  
    46.                 orderedQuery = (index == 0) ? query.OrderByDescending(expression) :  
    47.                     orderedQuery.ThenByDescending(expression);  
    48.             }  
    49.         }  
    50.         query = orderedQuery;  
    51.         return query;  
    52.     }  
    53. }  
  2. Create Second class GridSort,
    1. public class GridSort  
    2. {  
    3.     public string Field  
    4.     {  
    5.         get;  
    6.         set;  
    7.     }  
    8.     public string Dir  
    9.     {  
    10.         get;  
    11.         set;  
    12.     }  
    13. }  
  3. Get data,
    1. List<Student> data = new List<Student>();  
    2. data = GetStudentData();  
    3. List<GridSort> sort = new List<GridSort>()  
    4. sort = List of sort Expression  
  4. Apply sorting on data,
    1. data.MultipleSort(sort)