dotnet-dynamic-linq/PoweredSoft.DynamicLinq/Fluent/QueryBuilder/QueryBuilder.cs

116 lines
3.4 KiB
C#
Raw Normal View History

2018-02-11 20:55:29 -05:00
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class QueryBuilder<T> : QueryBuilderBase
2018-02-11 20:55:29 -05:00
{
public IQueryable<T> Query { get; set; }
public Type QueryableType { get; set; }
2018-02-11 20:55:29 -05:00
public QueryBuilder(IQueryable<T> query)
{
Query = query;
}
2018-02-12 04:30:55 -05:00
public virtual IQueryable<T> Build()
{
// the query.
var query = Query;
2018-02-12 05:18:44 -05:00
// build the filters.
2018-02-12 04:33:51 -05:00
query = BuildFilters(query);
2018-02-12 04:30:55 -05:00
2018-02-12 05:18:44 -05:00
// build the sorts
query = BuildSorts(query);
2018-02-12 04:30:55 -05:00
return query;
}
2018-02-12 05:18:44 -05:00
protected virtual IQueryable<T> BuildSorts(IQueryable<T> query)
{
Sorts.ForEach(sort =>
{
2018-02-20 22:21:23 -05:00
query = QueryableHelpers.CreateSortExpression(query, sort.Path, sort.sortDirection, sort.AppendSort);
2018-02-12 05:18:44 -05:00
});
return query;
}
2018-02-12 04:33:51 -05:00
protected virtual IQueryable<T> BuildFilters(IQueryable<T> query)
2018-02-11 20:55:29 -05:00
{
2018-02-12 05:18:44 -05:00
if (Filters == null || Filters?.Count() == 0)
return query;
// shared parameter.
var sharedParameter = Expression.Parameter(typeof(T), "t");
// build the expression.
var filterExpressionMerged = BuildFilterExpression(sharedParameter, Filters);
// make changes on the query.
query = query.Where(filterExpressionMerged);
2018-02-12 05:18:44 -05:00
2018-02-11 20:55:29 -05:00
return query;
}
protected virtual Expression<Func<T, bool>> BuildFilterExpression(ParameterExpression parameter, List<QueryBuilderFilter> filters)
2018-02-11 20:55:29 -05:00
{
Expression<Func<T, bool>> temp = null;
2018-02-11 20:55:29 -05:00
2018-02-12 04:35:27 -05:00
filters.ForEach(filter =>
2018-02-11 20:55:29 -05:00
{
Expression<Func<T, bool>> innerExpression;
if (filter.Filters?.Any() == true)
innerExpression = BuildFilterExpression(parameter, filter.Filters);
2018-02-11 20:55:29 -05:00
else
2018-02-12 04:35:27 -05:00
innerExpression = BuildFilterExpression(parameter, filter);
2018-02-11 20:55:29 -05:00
if (temp == null)
{
temp = innerExpression;
}
2018-02-11 20:55:29 -05:00
else
{
if (filter.And)
temp = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(temp.Body, innerExpression.Body), parameter);
else
temp = Expression.Lambda<Func<T, bool>>(Expression.OrElse(temp.Body, innerExpression.Body), parameter);
}
2018-02-11 20:55:29 -05:00
});
return temp;
2018-02-11 20:55:29 -05:00
}
protected virtual Expression<Func<T, bool>> BuildFilterExpression(ParameterExpression parameter, QueryBuilderFilter filter)
2018-02-11 20:55:29 -05:00
{
var ret = QueryableHelpers.CreateFilterExpression<T>(
filter.Path,
filter.ConditionOperator,
filter.Value,
filter.ConvertStrategy,
filter.CollectionHandling,
parameter: parameter,
2018-03-06 20:43:49 -05:00
nullChecking: IsNullCheckingEnabled,
stringComparision: filter.StringComparisation
);
return ret;
2018-02-11 20:55:29 -05:00
}
protected override QueryBuilderBase GetSubQueryBuilder()
{
return new QueryBuilder<T>(Query);
}
2018-02-11 20:55:29 -05:00
}
}