mirror of
https://github.com/Biarity/Sieve.git
synced 2025-10-22 05:15:30 +02:00
Basic working & complete tests
This commit is contained in:
27
Sieve/Extensions/OrderByDynamic.cs
Normal file
27
Sieve/Extensions/OrderByDynamic.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace Sieve.Extensions
|
||||
{
|
||||
public static partial class LinqExtentions
|
||||
{
|
||||
public static IQueryable<TEntity> OrderByDynamic<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
|
||||
bool desc, bool useThenBy)
|
||||
{
|
||||
string command = desc ?
|
||||
( useThenBy ? "ThenByDescending" : "OrderByDescending") :
|
||||
( useThenBy ? "ThenBy" : "OrderBy");
|
||||
var type = typeof(TEntity);
|
||||
var property = type.GetProperty(orderByProperty);
|
||||
var parameter = Expression.Parameter(type, "p");
|
||||
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
|
||||
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
|
||||
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
|
||||
source.Expression, Expression.Quote(orderByExpression));
|
||||
return source.Provider.CreateQuery<TEntity>(resultExpression);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace Sieve.Extensions
|
||||
{
|
||||
|
||||
public static class LinqExtensions
|
||||
{
|
||||
public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>
|
||||
(this IEnumerable<TSource> source,
|
||||
Func<TSource, TKey> keySelector,
|
||||
bool descending)
|
||||
{
|
||||
return descending ? source.OrderByDescending(keySelector)
|
||||
: source.OrderBy(keySelector);
|
||||
}
|
||||
|
||||
public static IOrderedQueryable<TSource> OrderByWithDirection<TSource, TKey>
|
||||
(this IQueryable<TSource> source,
|
||||
Expression<Func<TSource, TKey>> keySelector,
|
||||
bool descending)
|
||||
{
|
||||
return descending ? source.OrderByDescending(keySelector)
|
||||
: source.OrderBy(keySelector);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user