diff --git a/VirtoCommerce.Platform.Core/Extensions/IQueryableExtensions.cs b/VirtoCommerce.Platform.Core/Extensions/IQueryableExtensions.cs index 1e322fd3c..6ad0e550b 100644 --- a/VirtoCommerce.Platform.Core/Extensions/IQueryableExtensions.cs +++ b/VirtoCommerce.Platform.Core/Extensions/IQueryableExtensions.cs @@ -1,107 +1,27 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Linq.Expressions; -using System.Reflection; -using System.Collections; +using System.Linq.Dynamic.Core; namespace VirtoCommerce.Platform.Core.Common { - public static class IQueryableExtensions - { - public static IOrderedQueryable OrderBy(this IQueryable source, string property) - { - return ApplyOrder(source, property, "OrderBy"); - } - public static IOrderedQueryable OrderByDescending(this IQueryable source, string property) - { - return ApplyOrder(source, property, "OrderByDescending"); - } - public static IOrderedQueryable ThenBy(this IOrderedQueryable source, string property) - { - return ApplyOrder(source, property, "ThenBy"); - } - public static IOrderedQueryable ThenByDescending(this IOrderedQueryable source, string property) - { - return ApplyOrder(source, property, "ThenByDescending"); - } - - + public static class IQueryableExtensions + { public static IOrderedQueryable OrderBySortInfos(this IQueryable source, IEnumerable sortInfos) { - if(sortInfos.IsNullOrEmpty()) - { - throw new ArgumentNullException("sortInfos"); - } - IOrderedQueryable retVal = null; - var firstSortInfo = sortInfos.First(); - if (firstSortInfo.SortDirection == SortDirection.Descending) + if (sortInfos.IsNullOrEmpty()) { - retVal = source.OrderByDescending(firstSortInfo.SortColumn); + throw new ArgumentNullException(nameof(sortInfos)); } - else - { - retVal = source.OrderBy(firstSortInfo.SortColumn); - } - return retVal.ThenBySortInfos(sortInfos.Skip(1).ToArray()); + + var orderString = string.Join(",", sortInfos.Select(GetSortString)); + return source.OrderBy(orderString); } - public static IOrderedQueryable ThenBySortInfos(this IOrderedQueryable source, SortInfo[] sortInfos) + private static string GetSortString(SortInfo sortInfo) { - var retVal = source; - if (!sortInfos.IsNullOrEmpty()) - { - foreach (var sortInfo in sortInfos) - { - if (sortInfo.SortDirection == SortDirection.Descending) - { - retVal = retVal.ThenByDescending(sortInfo.SortColumn); - } - else - { - retVal = retVal.ThenBy(sortInfo.SortColumn); - } - } - } - return retVal; + var direction = sortInfo.SortDirection == SortDirection.Ascending ? "asc" : "desc"; + return $"{sortInfo.SortColumn} {direction}"; } - - public static IOrderedQueryable ApplyOrder(IQueryable source, string property, string methodName) - { - if (property == null) - throw new ArgumentNullException("property"); - - string[] props = property.Split('.'); - Type type = typeof(T); - ParameterExpression arg = Expression.Parameter(type, "x"); - Expression expr = arg; - foreach (string prop in props) - { - // use reflection (not ComponentModel) to mirror LINQ - PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - if (pi != null) - { - expr = Expression.Property(expr, pi); - type = pi.PropertyType; - } - else - { - return source.OrderBy(x => 1); - } - } - Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type); - LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); - - object result = typeof(Queryable).GetMethods().Single( - method => method.Name == methodName - && method.IsGenericMethodDefinition - && method.GetGenericArguments().Length == 2 - && method.GetParameters().Length == 2) - .MakeGenericMethod(typeof(T), type) - .Invoke(null, new object[] { source, lambda }); - return (IOrderedQueryable)result; - } - - } + } } diff --git a/VirtoCommerce.Platform.Core/VirtoCommerce.Platform.Core.csproj b/VirtoCommerce.Platform.Core/VirtoCommerce.Platform.Core.csproj index bf61d2b80..cf54a4478 100644 --- a/VirtoCommerce.Platform.Core/VirtoCommerce.Platform.Core.csproj +++ b/VirtoCommerce.Platform.Core/VirtoCommerce.Platform.Core.csproj @@ -17,6 +17,7 @@ +