public static IQueryable <T> OrderBy <T>(this IQueryable <T> query, ISortingCriteria sortingCriteria) { return(query.OrderByPropertyName(sortingCriteria.SortColunName, sortingCriteria.Ascending)); }
// Took inspiration from here on how to accomplish this // https://www.c-sharpcorner.com/article/dynamic-sorting-orderby-based-on-user-preference/ public static IQueryable <T> OrderBySortingCriteria <T>(this IQueryable <T> source, ISortingCriteria sortingCriteria, bool isNested = false) { if (string.IsNullOrEmpty(sortingCriteria.PropertyName)) { return(source); } var parameter = Expression.Parameter(source.ElementType, ""); var property = Expression.Property(parameter, sortingCriteria.PropertyName); var lambda = Expression.Lambda(property, parameter); var methodName = $"{(!isNested ? "Order" : "Then")}By{(sortingCriteria.IsAscending ? "" : "Descending")}"; var methodCallExpression = Expression.Call(typeof(Queryable), methodName, new [] { source.ElementType, property.Type }, source.Expression, Expression.Quote(lambda)); return(source.Provider.CreateQuery <T>(methodCallExpression)); }