public Expression <Func <T, bool> > GetExpression(IList <ExpressionFilter> filterList) { if (filterList.Count == 0) { throw new ArgumentException(string.Format(CollectionCannotBeNullOrEmpty, FilterListArgument), FilterListArgument); } ParameterExpression param = Expression.Parameter(typeof(T), "t"); Expression exp = null; LogicalOperator op = LogicalOperator.And; switch (filterList.Count) { case 1: exp = this.GetExpression(param, filterList[0]); break; case 2: exp = this.GetExpression(param, filterList[0], filterList[1]); break; default: while (filterList.Count > 0) { ExpressionFilter f1 = filterList[0]; ExpressionFilter f2 = filterList[1]; exp = exp == null ? this.GetExpression(param, filterList[0], filterList[1]) : op == LogicalOperator.Or ? Expression.OrElse(exp, this.GetExpression(param, filterList[0], filterList[1])) : Expression.AndAlso(exp, this.GetExpression(param, filterList[0], filterList[1])); op = filterList[1].LogicalOperator; filterList.Remove(f1); filterList.Remove(f2); if (filterList.Count != 1) { continue; } exp = Expression.AndAlso(exp, this.GetExpression(param, filterList[0])); filterList.RemoveAt(0); } break; } return(exp != null?Expression.Lambda <Func <T, bool> >(exp, param) : null); }
private BinaryExpression GetExpression(ParameterExpression param, ExpressionFilter filter1, ExpressionFilter filter2) { if (param == null) { throw new ArgumentNullException(ParamArgument, string.Format(ArgumentCannotBeNull, ParamArgument)); } if (filter1 == null) { throw new ArgumentNullException(Filter1Argument, string.Format(ArgumentCannotBeNull, Filter1Argument)); } if (filter2 == null) { throw new ArgumentNullException(Filter2Argument, string.Format(ArgumentCannotBeNull, Filter2Argument)); } Expression bin1 = this.GetExpression(param, filter1); Expression bin2 = this.GetExpression(param, filter2); return(filter1.LogicalOperator == LogicalOperator.Or ? Expression.OrElse(bin1, bin2) : Expression.AndAlso(bin1, bin2)); }
private Expression GetExpression(ParameterExpression param, ExpressionFilter filter) { if (param == null) { throw new ArgumentNullException(nameof(param)); } if (this.type != typeof(JObject)) { PropertyInfo propertyInfo = this.propertyList.FirstOrDefault(p => string.Compare(p.Name, filter.Property, StringComparison.InvariantCultureIgnoreCase) == 0); if (propertyInfo != null) { MemberExpression member = Expression.Property(param, filter.Property); ConstantExpression constant = filter.Value != null ? Expression.Constant(filter.Value, Nullable.GetUnderlyingType(filter.Value.GetType()) ?? filter.Value.GetType()) : Expression.Constant(filter.Value); Expression left = propertyInfo.PropertyType != typeof(DateTime?) ? member : (Expression)Expression.Call(member, this.getValueOrDefault); switch (filter.Operator) { case Operator.Equal: return(Expression.Equal(left, constant)); case Operator.NotEqual: return(Expression.NotEqual(left, constant)); case Operator.GreaterThan: return(Expression.GreaterThan(left, constant)); case Operator.GreaterThanOrEqual: return(Expression.GreaterThanOrEqual(left, constant)); case Operator.LessThan: return(Expression.LessThan(left, constant)); case Operator.LessThanOrEqual: return(Expression.LessThanOrEqual(left, constant)); case Operator.Contains: return(Expression.Call(left, this.containsMethod, constant)); case Operator.StartsWith: return(Expression.Call(left, this.startsWithMethod, constant)); case Operator.EndsWith: return(Expression.Call(left, this.endsWithMethod, constant)); } } } else { IndexExpression member = Expression.Property( param, typeof(JObject).GetProperty("Item", new[] { typeof(string) }), Expression.Constant(filter.Property, typeof(string))); //var value = Expression.Call(member, typeof (JToken).GetMethod("Value").MakeGenericMethod(typeof(string))); ConstantExpression constant = filter.Value != null ? Expression.Constant(filter.Value, Nullable.GetUnderlyingType(filter.Value.GetType()) ?? filter.Value.GetType()) : Expression.Constant(filter.Value); MethodInfo changeTypeMethod = typeof(Convert).GetMethod("ChangeType", new[] { typeof(object), typeof(Type) }); Expression value = Expression.Call(changeTypeMethod, member, Expression.Constant(typeof(string))); //Expression.Call(member, typeof(JToken).GetMethod("ToString", new Type[] { })); value = Expression.Convert(value, typeof(string)); if (filter.Value is double) { MethodInfo parseMethod = typeof(double).GetMethod("Parse", new[] { typeof(string) }); value = Expression.Call(parseMethod, value); } Expression left = value; switch (filter.Operator) { case Operator.Equal: return(Expression.Equal(left, constant)); case Operator.NotEqual: return(Expression.NotEqual(left, constant)); case Operator.GreaterThan: return(Expression.GreaterThan(left, constant)); case Operator.GreaterThanOrEqual: return(Expression.GreaterThanOrEqual(left, constant)); case Operator.LessThan: return(Expression.LessThan(left, constant)); case Operator.LessThanOrEqual: return(Expression.LessThanOrEqual(left, constant)); case Operator.Contains: return(Expression.Call(left, this.containsMethod, constant)); case Operator.StartsWith: return(Expression.Call(left, this.startsWithMethod, constant)); case Operator.EndsWith: return(Expression.Call(left, this.endsWithMethod, constant)); } } return(null); }