public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { JObject jObject = JObject.Load(reader); FilterParameterCollection result = null; IFilterOperatorResolver opres = null; IFilterSortPropResolver props = null; foreach (var prop in jObject.Properties()) { if (prop.Name == "params") { var st = new JsonSerializerSettings(); st.Converters.Add(new FilterParameterJsonConverter()); result = JsonConvert.DeserializeObject <FilterParameterCollection>((string)prop.Value, st); } else if (prop.Name == typeof(IFilterOperatorResolver).Name) { Type opresType = Type.GetType((string)prop.Value); opres = Activator.CreateInstance(opresType) as IFilterOperatorResolver; } else if (prop.Name == typeof(IFilterSortPropResolver).Name) { Type propsType = Type.GetType((string)prop.Value); props = Activator.CreateInstance(propsType) as IFilterSortPropResolver; } } if (result != null) { if (opres != null) { result.OperatorActionResolver = opres; } if (props != null) { result.PropertyNameResolver = props; } } return(result); } catch (Exception ex) { throw ex; } }
public override Expression <Func <T, bool> > GetLinqCondition <T>(IFilterSortPropResolver propResolver, IFilterOperatorResolver opResolver) { Expression body = null; ParameterExpression mainObject = Expression.Parameter(typeof(T), "obj"); foreach (FilterParameter p in this) { if (p.Value == null) { continue; } Expression value = Expression.Constant(p.Value, p.Value.GetType()); Expression propVal = mainObject; Type objType = typeof(T); IEnumerable <LambdaExpression> exprList = null; if (propResolver != null) { exprList = propResolver.FindResolution(_modelType ?? objType, objType, ListControlActionType.Filtering, p.ColumnName); } if (exprList != null && exprList.Count() > 0) { var modifier = new MainObjectLinkModifier(mainObject); propVal = modifier.Visit(exprList.ElementAt(0).Body); } else { string[] propTree = p.ColumnName.Split('.'); foreach (string prop in propTree) { PropertyInfo pi = objType.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public); if (pi != null) { propVal = Expression.Property(propVal, pi); objType = pi.PropertyType; } } } Expression boolOperation = null; if (opResolver != null) { boolOperation = opResolver.FindResolution(p.Operator, propVal, value); } if (boolOperation != null) { if (body == null) { body = boolOperation; } else { if (string.IsNullOrEmpty(p.OrGroupName) == false) { body = Expression.Or(body, boolOperation); } else { body = Expression.And(body, boolOperation); } } } } if (body == null) { body = Expression.Equal(Expression.Constant(1), Expression.Constant(1)); } else { var modifier = new ConstantModifier(); if (opResolver != null) { modifier.StayConstants = opResolver.GetStayConstants(); } body = modifier.Visit(body); } Expression <Func <T, bool> > res = Expression.Lambda <Func <T, bool> >(body, mainObject); return(res); }
public abstract Expression <Func <T, bool> > GetLinqCondition <T>(IFilterSortPropResolver propResolver, IFilterOperatorResolver opResolver) where T : class, new();
public static IEnumerable <T> Filtering <T>(this IEnumerable <T> list, FilterParameterCollection filters, IFilterSortPropResolver propResolver, IFilterOperatorResolver opResolver) where T : class, new() { if (filters != null) { return(list.Where(filters.GetLinqCondition <T>(propResolver, opResolver).Compile())); } return(list); }
public static IQueryable <T> Filtering <T>(this IQueryable <T> list, FilterParameterCollection filters, IFilterOperatorResolver opResolver) where T : class, new() { if (filters != null) { return(list.Where(filters.GetLinqCondition <T>(filters.PropertyNameResolver, opResolver))); } return(list); }