public static Expression GetConstantExpression(FilterMetaRecord meta, string val) { if (meta.ParametrType.IsEnum) { return(Expression.Constant(Enum.Parse(meta.ParametrType, val))); } else { return(Expression.Constant(Convert.ChangeType(val, meta.ParametrType))); } }
public static Expression GetFirstExpression(FilterMetaRecord meta, Expression valExperssion, Expression propertyAccess) { Expression res = null; switch (meta.OperatorName) { case Operators.Equal: { res = Expression.Equal(propertyAccess, valExperssion); break; } case Operators.Contains: { res = Expression.Call(propertyAccess, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), valExperssion); break; } } return(res); }
public static Expression GetNextExpression(Expression condition, FilterMetaRecord meta, Expression ValExpression, Expression propertyAccess) { Expression res = null; switch (meta.OperatorBetween) { case Operators_between.Or: { res = Expression.Or(condition, GetFirstExpression(meta, ValExpression, propertyAccess)); break; } case Operators_between.And: { res = Expression.And(condition, GetFirstExpression(meta, ValExpression, propertyAccess)); break; } } return(res); }
public static IQueryable <T> FilterByFilterMetaRecord <T>(IQueryable <T> items, FilterRecord filter, FilterMetaRecord meta) { var param = Expression.Parameter(typeof(T), "x"); var propertyAccess = meta.Path.Split('.').Aggregate((Expression)param, Expression.Property); var constValue = GetConstantExpression(meta, filter.Value[0]); var condition = GetFirstExpression(meta, constValue, propertyAccess); if (!meta.IsSingleFilter) { for (int i = 1; i < filter.Value.Count; i++) { var now_val_next = GetConstantExpression(meta, filter.Value[i]); condition = GetNextExpression(condition, meta, now_val_next, propertyAccess); } } return(items.Where(Expression.Lambda <Func <T, bool> >(condition, param))); }
public static IOrderedQueryable <T> ThenSortingBySortingMetaRecord <T>(IOrderedQueryable <T> items, SortingRecord sort, FilterMetaRecord meta) { var param = Expression.Parameter(typeof(T), "x"); var propertyAccess = meta.Path.Split('.').Aggregate((Expression)param, Expression.Property); if (sort.Direction == "asc") { return((IOrderedQueryable <T>)items.Provider.CreateQuery(Expression.Call(typeof(Queryable), "ThenBy", new Type[] { typeof(T), meta.ParametrType }, items.Expression, Expression.Lambda(propertyAccess, param)))); } else { return((IOrderedQueryable <T>)items.Provider.CreateQuery(Expression.Call(typeof(Queryable), "ThenByDescending", new Type[] { typeof(T), meta.ParametrType }, items.Expression, Expression.Lambda(propertyAccess, param)))); } }