Skip to content
This repository has been archived by the owner on Oct 11, 2019. It is now read-only.

IQueryableExtensions use System.Lynq.Dinamic #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 12 additions & 92 deletions VirtoCommerce.Platform.Core/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}


public static class IQueryableExtensions
{
public static IOrderedQueryable<T> OrderBySortInfos<T>(this IQueryable<T> source, IEnumerable<SortInfo> sortInfos)
{
if(sortInfos.IsNullOrEmpty())
{
throw new ArgumentNullException("sortInfos");
}
IOrderedQueryable<T> 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<T> ThenBySortInfos<T>(this IOrderedQueryable<T> 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<T> ApplyOrder<T>(IQueryable<T> 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<T>)result;
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.6" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="2.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down