public FilterOperatorAttribute(FilterOperatorViewModel oper, string dbField = null)
 {
     this.Operator = oper;
     this.DbField  = dbField;
 }
Exemplo n.º 2
0
        public static IQueryable <T> ProcessDinamicQuery <T, P>(
            this IQueryable <T> source, GridRequestViewModel <P> requestModel, char propertyNavigationSplitter = '_') where P : new()
        {
            var  query    = source;
            bool isSorted = false;

            foreach (var aProperty in requestModel.Search.PredicateObject.GetType().GetProperties())
            {
                if (aProperty.GetValue(requestModel.Search.PredicateObject) == null)
                {
                    continue;
                }
                var param = Expression.Parameter(typeof(T), "p");

                if (typeof(P).GetProperty(aProperty.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).PropertyType == typeof(string))
                {
                    string[] props = aProperty.Name.Split(propertyNavigationSplitter);
                    if (!PropertyExists <T>(props[0]))
                    {
                        continue;
                    }
                    Type type = typeof(T);
                    ParameterExpression arg  = Expression.Parameter(type, "x");
                    Expression          expr = arg;
                    foreach (string prop in props)
                    {
                        PropertyInfo pi = type.GetProperty(prop);
                        expr = Expression.Property(expr, pi);
                        type = pi.PropertyType;
                    }

                    var attr = (FilterOperatorAttribute[])aProperty.GetCustomAttributes(typeof(FilterOperatorAttribute), false);
                    if (attr.Length > 0)
                    {
                        if (!string.IsNullOrEmpty(attr[0].DbField))
                        {
                            expr = Expression.Property(arg, attr[0].DbField);
                        }
                    }
                    var value = Expression.Constant(aProperty.GetValue(requestModel.Search.PredicateObject));

                    var containsmethod = type.GetMethod("Contains", new[] { typeof(string) });
                    var call           = Expression.Call(expr, containsmethod, value);
                    var lambda         = Expression.Lambda <Func <T, bool> >(call, arg);
                    query = query.Where(lambda);
                }
                else
                {
                    string[] props = aProperty.Name.Split(propertyNavigationSplitter);
                    if (!PropertyExists <T>(props[0]))
                    {
                        continue;
                    }
                    Type type = typeof(T);
                    ParameterExpression arg  = Expression.Parameter(type, "x");
                    Expression          expr = arg;
                    foreach (string prop in props)
                    {
                        PropertyInfo pi = type.GetProperty(prop);
                        expr = Expression.Property(expr, pi);
                        type = pi.PropertyType;
                    }
                    var value = Expression.Constant(aProperty.GetValue(requestModel.Search.PredicateObject));

                    FilterOperatorViewModel _operator = FilterOperatorViewModel.Equals;
                    var attr = (FilterOperatorAttribute[])aProperty.GetCustomAttributes(typeof(FilterOperatorAttribute), false);
                    if (attr.Length > 0)
                    {
                        if (attr[0].Operator != null)
                        {
                            _operator = attr[0].Operator.Value;
                        }
                        if (!string.IsNullOrEmpty(attr[0].DbField))
                        {
                            expr = Expression.Property(arg, attr[0].DbField);
                        }
                    }

                    if (typeof(P).GetProperty(aProperty.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).PropertyType == typeof(DateTime?))
                    {
                        var dateVal = ((DateTime)aProperty.GetValue(requestModel.Search.PredicateObject)).AddDays(1).AddTicks(-1);
                        if (_operator == FilterOperatorViewModel.GreaterOrEquals)
                        {
                            dateVal = dateVal.AddDays(-1);
                        }
                        value = Expression.Constant(dateVal);
                    }

                    BinaryExpression binaryExpression = Expression.Equal(Expression.Property(arg, aProperty.Name), value);
                    if (_operator == FilterOperatorViewModel.GreaterOrEquals)
                    {
                        binaryExpression = Expression.GreaterThanOrEqual(Expression.Property(arg, aProperty.Name), value);
                    }
                    if (_operator == FilterOperatorViewModel.LessOrEquals)
                    {
                        binaryExpression = Expression.LessThanOrEqual(Expression.Property(arg, aProperty.Name), value);
                    }

                    var exp = Expression.Lambda <Func <T, bool> >(binaryExpression, arg);
                    query = query.Where(exp);
                }
            }

            if (!string.IsNullOrEmpty(requestModel.Sort.Predicate))
            {
                isSorted = true;
                if (requestModel.Sort.Reverse)
                {
                    query = query.OrderByDescending(requestModel.Sort.Predicate);
                }
                else
                {
                    query = query.OrderBy(requestModel.Sort.Predicate);
                }
            }
            if (!isSorted)
            {
                query = query.OrderBy("Id");
            }
            return(query);
        }