Commit 5c5e2a62 authored by thienvo's avatar thienvo

test optimize get all by sieve

parent 9d315d87
...@@ -158,7 +158,89 @@ namespace Sieve.Services ...@@ -158,7 +158,89 @@ namespace Sieve.Services
} }
} }
} }
//public Expression GetExpressionQuery<TEntity>(TSieveModel model,)
public Expression<Func<TEntity, bool>> GetExpressionQuery<TEntity>(
TSieveModel model,
bool bCaseSensitive = false)
{
if (model?.GetFiltersParsed() == null)
{
return null;
}
Expression outerExpression = null;
var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.GetFiltersParsed())
{
Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names)
{
var (fullName, property) = GetSieveProperty<TEntity>(false, true, filterTermName);
if (property != null)
{
var converter = TypeDescriptor.GetConverter(property.PropertyType);
dynamic propertyValue = parameterExpression;
foreach (var part in fullName.Split('.'))
{
propertyValue = Expression.PropertyOrField(propertyValue, part);
}
if (filterTerm.Values == null) continue;
foreach (var filterTermValue in filterTerm.Values)
{
dynamic constantVal = converter.CanConvertFrom(typeof(string))
? converter.ConvertFrom(filterTermValue)
: Convert.ChangeType(filterTermValue, property.PropertyType);
Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
if (filterTerm.OperatorIsCaseInsensitive)
{
propertyValue = Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
filterValue = Expression.Call(filterValue,
typeof(string).GetMethods()
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
}
var expression = GetExpression(filterTerm, filterValue, propertyValue, bCaseSensitive);
if (filterTerm.OperatorIsNegated)
{
expression = Expression.Not(expression);
}
if (innerExpression == null)
{
innerExpression = expression;
}
else
{
innerExpression = Expression.Or(innerExpression, expression);
}
}
}
}
if (outerExpression == null)
{
outerExpression = innerExpression;
continue;
}
if (innerExpression == null)
{
continue;
}
outerExpression = Expression.And(outerExpression, innerExpression);
}
return Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression);
}
private IQueryable<TEntity> ApplyFiltering<TEntity>( private IQueryable<TEntity> ApplyFiltering<TEntity>(
TSieveModel model, TSieveModel model,
IQueryable<TEntity> result, IQueryable<TEntity> result,
...@@ -337,7 +419,11 @@ namespace Sieve.Services ...@@ -337,7 +419,11 @@ namespace Sieve.Services
var page = model?.Page ?? 1; var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize; var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize; var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize;
if (ResultCountBeForeApplyPagination==0)
{
ResultCountBeForeApplyPagination = result.Count(); ResultCountBeForeApplyPagination = result.Count();
}
if (pageSize > 0) if (pageSize > 0)
{ {
result = result.Skip((page - 1) * pageSize); result = result.Skip((page - 1) * pageSize);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment