private Expression GetFilterExpression(Operation.Condition cond) { if (IsFilter(cond.Constant) && FilterValues != null) { var filterProp = cond.Constant.Remove(0, 1); ViewMappingHelper helper = new ViewMappingHelper(new ViewMappingAttribute(filterProp)); var value = Expression.Lambda( Expression.Block( ExpressionHelpers.ParseProperty(LinqToSql, Expression.Constant(FilterValues), FilterValues.GetType(), cond.PropertyExpression.Type, helper, 0, FilterValues, true))).Compile().DynamicInvoke(); if (this.IsTypeSame(cond)) return Expression.Constant(value, cond.PropertyExpression.Type); return Expression.Constant(value); } else if (FilterValues == null && IsFilter(cond.Constant)) throw new NullReferenceException("Filter Object cannot be null if the View Requires it"); Type parameterType; if (cond.Operator == FilterOperators.Contains) { if (cond.PropertyExpression.Type == typeof(String)) parameterType = cond.PropertyExpression.Type; else parameterType = cond.PropertyExpression.Type.GetGenericArguments().Single(); } else parameterType = cond.PropertyExpression.Type; Object constant; if (typeof(Enum).IsAssignableFrom(parameterType)) { var enumInt = 0; if (int.TryParse(cond.Constant, out enumInt)) constant = Enum.ToObject(parameterType, enumInt); else constant = Enum.Parse(parameterType, cond.Constant); } else constant = cond.Constant; if (parameterType.IsNullable() && parameterType.IsSimpleType() && parameterType.IsValueType) return Expression.Constant(constant.ToString().ToNullable(parameterType), parameterType); else return Expression.Constant(Convert.ChangeType(constant, parameterType)); }
private IEnumerable<Operation> BuildOperations(String filterString) { List<String> stringOperations = filterString.Split(':').ToList(); for (int i = 0; i < stringOperations.Count; i = i + 1) { if (stringOperations.Count >= i + 3) { Operation opp = new Operation(); if (i > 0) { opp.Operator = stringOperations[i++]; } Type outModel = ViewModel; var propertyString = stringOperations[i++]; ViewMappingHelper helper = new ViewMappingHelper(new ViewMappingAttribute(propertyString)); opp.Filter.PropertyExpression = ExpressionHelpers.ParseProperty(LinqToSql, ParameterExpression, ViewModel, typeof(Object), helper, 0, null, true); opp.Filter.Operator = stringOperations[i++]; var constantString = stringOperations[i]; opp.Filter.Constant = constantString; yield return opp; } else throw new Exception("Invalid Filter"); } }