internal Expression <Func <T, bool> > GetExpression(IFilterStatement statement) { var builder = new FilterBuilder(new BuilderHelper()); var exp = builder.GetExpression <T>(this); return(exp); }
private Expression GetExpression(ParameterExpression param, IFilterStatement statement, string propertyName = null) { Expression resultExpr = null; var memberName = propertyName ?? statement.PropertyId; Expression member = helper.GetMemberExpression(param, memberName); if (Nullable.GetUnderlyingType(member.Type) != null && statement.Value != null) { resultExpr = Expression.PropertyOrField(member, "HasValue"); member = Expression.PropertyOrField(member, "Value"); } var safeStringExpression = GetSafeStringExpression(member, statement); resultExpr = resultExpr != null?Expression.AndAlso(resultExpr, safeStringExpression) : safeStringExpression; resultExpr = GetSafePropertyMember(param, memberName, resultExpr); if ((statement.Operation == Operation.IsNull || statement.Operation == Operation.IsNullOrWhiteSpace) && memberName.Contains(".")) { resultExpr = Expression.OrElse(CheckIfParentIsNull(param, member, memberName), resultExpr); } return(resultExpr); }
public IFilterStatementConnection By <TPropertyType>(FilterStatement <TPropertyType> filter) { IFilterStatement statement = filter; _statements.Add(statement); return(new FilterStatementConnection <T>(this, statement)); }
private Expression GetSafeStringExpression(Expression member, IFilterStatement statement) { var operation = statement.Operation; if (member.Type != typeof(string)) { return(GetSafeExpression(member, statement)); } Expression newMember = member; if (operation != Operation.IsNullOrWhiteSpace && operation != Operation.IsNotNullNorWhiteSpace) { var trimMemberCall = Expression.Call(member, helper.trimMethod); newMember = Expression.Call(trimMemberCall, helper.toLowerMethod); } Expression resultExpr = operation != Operation.IsNull ? GetSafeExpression(newMember, statement) : GetSafeExpression(member, statement); if (member.Type == typeof(string) && new[] { Operation.IsNull, Operation.IsNullOrWhiteSpace, Operation.IsNotNullNorWhiteSpace }.Contains(operation) == false) { Expression memberIsNotNull = Expression.NotEqual(member, Expression.Constant(null)); resultExpr = Expression.AndAlso(memberIsNotNull, resultExpr); } return(resultExpr); }
private static Expression GetExpression(ParameterExpression param, IFilterStatement statement, string propertyName = null) { Expression resultExpr = null; var memberName = propertyName ?? statement.PropertyId; MemberExpression member = param.GetMemberExpression(memberName); if (Nullable.GetUnderlyingType(member.Type) != null && statement.Value != null) { resultExpr = Expression.Property(member, "HasValue"); member = Expression.Property(member, "Value"); } var constant1 = Expression.Constant(statement.Value); var constant2 = Expression.Constant(statement.Value2); CheckPropertyValueMismatch(member, constant1); var safeStringExpression = statement.Operation.GetExpression(member, constant1, constant2); resultExpr = resultExpr != null?Expression.AndAlso(resultExpr, safeStringExpression) : safeStringExpression; resultExpr = GetSafePropertyMember(param, memberName, resultExpr); if (statement.Operation.ExpectNullValues && memberName.Contains(".")) { resultExpr = Expression.OrElse(CheckIfParentIsNull(param, memberName), resultExpr); } return(resultExpr); }
public IFilterStatementConnection <TClass> By <TPropertyType>(string propertyName, Operation operation, TPropertyType value, FilterStatementConnector connector = FilterStatementConnector.And) { IFilterStatement statement = null; statement = new FilterStatement <TPropertyType>(propertyName, operation, value, connector); _statements.Add(statement); return(new FilterStatementConnection <TClass>(this, statement)); }
public IFilterStatementConnection <TClass> By <TPropertyType>(string propertyName, Operation operation, TPropertyType value, FilterStatementConnector connector = FilterStatementConnector.And, bool?isReplaceAllSpace = false) { _isReplaceAllSpace = isReplaceAllSpace; IFilterStatement statement = null; statement = new FilterStatement <TPropertyType>(propertyName, operation, value, connector); _statements.Add(statement); return(new FilterStatementConnection <TClass>(this, statement)); }
private Expression ProcessListStatement(ParameterExpression param, IFilterStatement statement) { var propertyName = statement.PropertyId.Replace("[", "").Replace("]", ""); var listItemParam = Expression.Parameter(param.Type, "i"); var lambda = Expression.Lambda(GetExpression(listItemParam, statement, propertyName), listItemParam); var member = helper.GetMemberExpression(param, propertyName); var enumerableType = typeof(Enumerable); var anyInfo = enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public) .First(m => m.Name == "Any" && m.GetParameters().Count() == 2); anyInfo = anyInfo.MakeGenericMethod(param.Type); return(Expression.Call(anyInfo, member, lambda)); }
static Expression GetExpression(ParameterExpression param, IFilterStatement statement, string propertyName = null) { Expression member = GetMemberExpression(param, propertyName ?? statement.PropertyName); Expression constant = Expression.Constant(statement.Value); if (statement.Value is string) { var trimMemberCall = Expression.Call(member, trimMethod); member = Expression.Call(trimMemberCall, toLowerMethod); var trimConstantCall = Expression.Call(constant, trimMethod); constant = Expression.Call(trimConstantCall, toLowerMethod); } return(Expressions[statement.Operation].Invoke(member, constant)); }
private static IList ValueList(IFilterStatement statement) { var type = statement.GetPropertyType(); var myList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(type), statement.Value); if (type == typeof(string)) { for (var itemIndex = 0; itemIndex < myList.Count; itemIndex++) { myList[itemIndex] = (myList[itemIndex] as string)?.Trim().ToLower(); } } return(myList); }
private Expression ProcessListStatement(ParameterExpression param, IFilterStatement statement) { var basePropertyName = statement.PropertyName.Substring(0, statement.PropertyName.IndexOf("[")); var propertyName = statement.PropertyName.Replace(basePropertyName, "").Replace("[", "").Replace("]", ""); var type = param.Type.GetProperty(basePropertyName).PropertyType.GetGenericArguments()[0]; ParameterExpression listItemParam = Expression.Parameter(type, "i"); var lambda = Expression.Lambda(GetExpression(listItemParam, statement, propertyName), listItemParam); var member = GetMemberExpression(param, basePropertyName); var enumerableType = typeof(Enumerable); var anyInfo = enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "Any" && m.GetParameters().Count() == 2); anyInfo = anyInfo.MakeGenericMethod(type); return(Expression.Call(anyInfo, member, lambda)); }
private Expression GetExpression(Expression parameter, IFilterStatement statement, string propertyName = null) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } if (statement == null) { throw new ArgumentNullException(nameof(statement)); } var memberName = propertyName ?? statement.Property; var member = this.GetMemberExpression(parameter, memberName); var constant = this.GetConstantExpression(statement.Value); var constant2 = this.GetConstantExpression(statement.Value2); Expression expression = null; if (Nullable.GetUnderlyingType(member.Type) != null && statement.Value != null) { member = Expression.Property(member, "Value"); expression = Expression.Property(member, "HasValue"); } var stringExpression = this.GetStringExpression(member, statement.Operation, constant, constant2); expression = expression != null?Expression.AndAlso(expression, stringExpression) : stringExpression; if (memberName.Contains(".")) { var parentName = memberName.Substring(0, memberName.IndexOf(".", StringComparison.Ordinal)); var parentMember = this.GetMemberExpression(parameter, parentName); expression = statement.Operation == Operation.IsNull || statement.Operation == Operation.IsNullOrWhiteSpace ? Expression.OrElse(Expression.Equal(parentMember, Expression.Constant(null)), expression) : Expression.AndAlso(Expression.NotEqual(parentMember, Expression.Constant(null)), expression); } return(expression); }
private Expression GetArrayExpression(Expression parameter, IFilterStatement statement) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } if (statement == null) { throw new ArgumentNullException(nameof(statement)); } var baseName = statement.Property.Substring(0, statement.Property.IndexOf("[", StringComparison.Ordinal)); var type = parameter.Type.GetProperty(baseName).PropertyType.GetGenericArguments()[0]; var name = statement.Property.Replace(baseName, "").Replace("[", "").Replace("]", ""); var methodInfo = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "Any" && m.GetParameters().Length == 2).MakeGenericMethod(type); var member = this.GetMemberExpression(parameter, baseName); var itemParameter = Expression.Parameter(type, "i"); var expression = Expression.Lambda(GetExpression(itemParameter, statement, name), itemParameter); return(Expression.Call(methodInfo, member, expression)); }
private Expression GetConstantExpression(Expression member, IFilterStatement statement) { var value = statement.Value; if (value == null) { return(null); } Expression constant = Expression.Constant(value); if (value is string) { var trimConstantCall = Expression.Call(constant, helper.trimMethod); constant = Expression.Call(trimConstantCall, helper.toLowerMethod); } else if (statement.ValueIsList()) { var myList = ValueList(statement); constant = Expression.Constant(statement.Operation == Operation.Between ? new ArrayList(myList).ToArray(statement.GetPropertyType()) : myList); } return(constant); }
private Expression GetSafeExpression(Expression member, IFilterStatement statement) { var operation = statement.Operation; var matchType = new OperationHelper().FetchMatchType(operation, statement.MatchType); var constant = GetConstantExpression(member, statement); if (operation != Operation.Between && statement.ValueIsList()) { if (operation == Operation.EqualTo && matchType == MatchType.Any) { return(Expressions[Operation.Contains].Invoke(member, constant)); } else if (operation == Operation.NotEqualTo && matchType == MatchType.All) { return(Expressions[Operation.DoesNotContain].Invoke(member, constant)); } else { var runningExpression = null as Expression; var myList = ValueList(statement); var connector = matchType == MatchType.Any ? Connector.Or : Connector.And; foreach (var item in myList) { Expression loopConstant = Expression.Constant(item); var loopExpression = Expressions[operation].Invoke(member, loopConstant); runningExpression = runningExpression == null ? loopExpression : CombineExpressions(runningExpression, loopExpression, connector); } return(runningExpression ?? Expression.Constant(true)); } } return(Expressions[operation].Invoke(member, constant)); }
private static bool IsList(IFilterStatement statement) => statement.PropertyId.Contains("[") && statement.PropertyId.Contains("]");
public FilterStatementConnection(IFilter <TClass> filter, IFilterStatement statement) { _filter = filter; _statement = statement; }
private static bool IsList(IFilterStatement statement) { return(statement.PropertyId.Contains("[") && statement.PropertyId.Contains("]")); }
internal Expression GetPartialExpression(ParameterExpression param, ref Connector connector, IFilterStatement statement) { Expression expr = null; if (IsList(statement)) { expr = ProcessListStatement(param, statement); } else { expr = GetExpression(param, statement); } connector = statement.Connector; return(expr); }
internal FilterStatementConnection(IFilter filter, IFilterStatement statement) { _filter = filter; _statement = statement; }
private static Expression GetExpression(ParameterExpression param, IFilterStatement statement, string propertyName = null, bool?isReplaceAllSpace = false) { Expression member = GetMemberExpression(param, propertyName ?? statement.PropertyName); Expression constant; if (statement.Value == null) { if (statement.Operation == Operation.IsEmpty || statement.Operation == Operation.IsNotEmpty) { return(Expressions[statement.Operation].Invoke(member, Expression.Constant(string.Empty))); } if (member.Type == typeof(DateTime)) { return(Expressions[statement.Operation].Invoke(member, Expression.Constant(DateTime.MinValue))); } return(Expressions[statement.Operation].Invoke(member, Expression.Constant(statement.Value))); } var currentStatementValueType = statement.Value.GetType(); if (currentStatementValueType == typeof(string)) { if (member.Type == typeof(string)) { constant = Expression.Constant(statement.Value, typeof(string)); var trimMemberCall = Expression.Call(member, trimMethod); if (isReplaceAllSpace.Value) { var toLowerCall = Expression.Call(trimMemberCall, toLowerMethod); var constant1 = Expression.Constant(" ", typeof(string)); var constant2 = Expression.Constant(string.Empty, typeof(string)); member = Expression.Call(toLowerCall, replaceMethod, constant1, constant2); } else { member = Expression.Call(trimMemberCall, toLowerMethod); } var trimConstantCall = Expression.Call(constant, trimMethod); constant = Expression.Call(trimConstantCall, toLowerMethod); } else { Type u = Nullable.GetUnderlyingType(member.Type); if (u != null && u == typeof(int)) { int?converted = statement.Value.ToString().ToNullableInt(); constant = Expression.Constant(converted); } else if (member.Type.GetTypeInfo().IsEnum) { constant = Expression.Constant(Enum.Parse(member.Type, statement.Value.ToString())); } else if (member.Type == typeof(DateTime)) { var converted = DateTime.Parse(statement.Value.ToString()).ToUniversalTime(); constant = Expression.Constant(converted); } else { var parseMethod = member.Type.GetMethod("Parse", new[] { typeof(string) }); var converted = parseMethod.Invoke(null, new[] { statement.Value }); constant = Expression.Constant(converted); } } } else { constant = Expression.Constant(statement.Value); var parseMethod = currentStatementValueType.GetMethod("Parse", new[] { typeof(string) }); UnaryExpression converted = Expression.Convert(member, typeof(decimal), parseMethod); return(Expressions[statement.Operation].Invoke(converted, constant)); } return(Expressions[statement.Operation].Invoke(member, constant)); }