using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace gehGassi.Common.Extensions { public static class OrderExtensions { public static IOrderedQueryable SpecialOrderBy(this IQueryable source, string propertyName, SortDirection sortDirection = SortDirection.Ascending) { if (sortDirection == SortDirection.Ascending) return source.OrderBy(ToLambda(propertyName)); return source.OrderByDescending(ToLambda(propertyName)); } public static IOrderedQueryable SpecialThenBy(this IOrderedQueryable source, string propertyName, SortDirection sortDirection = SortDirection.Ascending) { if (sortDirection == SortDirection.Ascending) return source.ThenBy(ToLambda(propertyName)); return source.ThenByDescending(ToLambda(propertyName)); } private static Expression> ToLambda(string propertyName) { var parameter = Expression.Parameter(typeof(T)); var property = Expression.Property(parameter, propertyName); var convertedProperty = Expression.Convert(property, typeof(object)); return Expression.Lambda>(convertedProperty, parameter); } } /// /// Sortierrichtung /// public enum SortDirection { /// /// Aufsteigend /// Ascending = 1, /// /// Absteigend /// Descending = 2 } }