Skip to content

Commit

Permalink
remove System.Linq.Dynamic.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Jan 22, 2025
1 parent 536404e commit f70e382
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="9.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Riok.Mapperly" Version="4.1.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.1" />
<PackageReference Include="Hangfire.Core" Version="1.8.17" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.0.0" />
<PackageReference Include="ActualLab.Fusion" Version="9.8.17" />
Expand Down
62 changes: 62 additions & 0 deletions src/Application/Common/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,66 @@ public static IQueryable<T> WhereContainsKeyword<T>(this IQueryable<T> source, s
var lambda = Expression.Lambda<Func<T, bool>>(predicate, parameter);
return source.Where(lambda);
}



#region OrderBy
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderByProperty)
{
var parts = orderByProperty.Split(' ', StringSplitOptions.RemoveEmptyEntries);
string property = parts[0];
string direction = parts.Length > 1 && parts[1].Equals("Descending", StringComparison.OrdinalIgnoreCase)
? "OrderByDescending"
: "OrderBy";

return ApplyOrder(source, property, direction);
}

public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string orderByProperty)
{
return ApplyOrder(source, orderByProperty, "OrderByDescending");
}

public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string orderByProperty)
{
return ApplyOrder(source, orderByProperty, "ThenBy");
}

public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string orderByProperty)
{
return ApplyOrder(source, orderByProperty, "ThenByDescending");
}

private static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
var type = typeof(T);
var propertyInfo = type.GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (propertyInfo == null)
{
throw new ArgumentException($"Property '{property}' does not exist on type '{type.Name}'.");
}

var parameter = Expression.Parameter(type, "x");
var propertyAccess = Expression.MakeMemberAccess(parameter, propertyInfo);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);

var resultExpression = Expression.Call(
typeof(Queryable),
methodName,
new[] { type, propertyInfo.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));

return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(resultExpression);
}

public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderBy, string sortDirection)
{
return sortDirection.Equals("Descending", StringComparison.OrdinalIgnoreCase)
? source.OrderByDescending(orderBy)
: source.OrderBy(orderBy);
}
#endregion


}
1 change: 0 additions & 1 deletion src/Application/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
global using System.ComponentModel;
global using System.Data;
global using System.Globalization;
global using System.Linq.Dynamic.Core;
global using System.Linq.Expressions;
global using System.Reflection;
global using System.Text.Json;
Expand Down

0 comments on commit f70e382

Please sign in to comment.