//public static IEnumerable<T> FilterBy<T, TKey>(
        //    this IEnumerable<T> items
        //  , Filter filter
        //  , Expression<Func<T, TKey>> keySelector)
        //{
        //  var fields = new FieldMap(filter);
        //  return DoFilterBy(items, fields, keySelector);
        //}

        //public static IEnumerable<T> ThenBy<T, TKey>(
        //    this IEnumerable<T> items
        //  , Filter filter
        //  , Expression<Func<T, TKey>> keySelector)
        //{
        //  var fields = new FieldMap(filter);
        //  return DoFilterBy(items, fields, keySelector);
        //}

        private static IEnumerable <T> DoFilterBy <T, TKey>(
            this IEnumerable <T> items
            , FieldMap fields
            , Expression <Func <T, TKey> > keySelector)
        {
            if (fields == null || fields.Count == 0)
            {
                return(items);
            }

            var expression = ExpressionUtils.FindMemberExpression(keySelector);

            if (expression == null)
            {
                throw new Exception("A expressão não é válida. Apenas um seletor de propriedade de objeto é suportado.");
            }

            var name  = expression.Member.Name;
            var field = fields.FirstOrDefault(x => x.Key.EqualsIgnoreCase(name));

            if (field.Key != null)
            {
                var comparison = CreateComparisonExpression <T>(field.Key, field.Value, forSql: false);
                items = items.Where(comparison.Compile());
            }

            return(items);
        }
        private static ISortableEnumerable <T> DoSortBy <T, TKey>(
            this IEnumerable <T> items
            , Sort sort
            , Expression <Func <T, TKey> > keySelector
            , bool reset)
        {
            if (sort == null)
            {
                return(new SortableEnumerable <T>(items));
            }

            var expression = ExpressionUtils.FindMemberExpression(keySelector);

            if (expression == null)
            {
                throw new Exception("A expressão não é válida. Apenas um seletor de propriedade de objeto é suportado.");
            }

            var name  = expression.Member.Name;
            var field = sort.GetSortedField(name);

            if (field?.Order == SortOrder.Ascending)
            {
                var source = (items as ISortableEnumerable <T>)?.Source ?? items;
                if (reset)
                {
                    items = source.OrderBy(keySelector.Compile());
                }
                else
                {
                    items =
                        (source is IOrderedEnumerable <T>)
              ? ((IOrderedEnumerable <T>)source).ThenBy(keySelector.Compile())
              : source.OrderBy(keySelector.Compile());
                }
            }

            if (field?.Order == SortOrder.Descending)
            {
                var source = (items as ISortableEnumerable <T>)?.Source ?? items;
                if (reset)
                {
                    items = source.OrderByDescending(keySelector.Compile());
                }
                else
                {
                    items =
                        (source is IOrderedEnumerable <T>)
              ? ((IOrderedEnumerable <T>)source).ThenByDescending(keySelector.Compile())
              : source.OrderByDescending(keySelector.Compile());
                }
            }

            return(new SortableEnumerable <T>(items));
        }
        public static Sort AddField <T>(this Sort sort, Expression <Func <T, object> > keySelector)
        {
            var expression = ExpressionUtils.FindMemberExpression(keySelector);

            if (expression == null)
            {
                throw new Exception("A expressão não é válida. Apenas um seletor de propriedade de objeto é suportado.");
            }

            var fieldName = expression.Member.Name;

            sort.Add(fieldName);
            return(sort);
        }
Beispiel #4
0
        private static void ExtractInfo <T>(
            Expression <Func <T, object> > keySelector
            , out string fieldName
            , out string fieldTitle
            , out string fieldDataType)
        {
            var expression = ExpressionUtils.FindMemberExpression(keySelector);

            if (expression == null)
            {
                throw new Exception("A expressão não é válida. Apenas um seletor de propriedade de objeto é suportado.");
            }

            var memberType =
                (expression.Member as PropertyInfo)?.PropertyType
                ?? (expression.Member as System.Reflection.FieldInfo)?.FieldType
                ?? typeof(string);

            fieldName     = Conventions.MakeName(expression.Member.Name);
            fieldTitle    = Conventions.MakeTitle(expression.Member.Name);
            fieldDataType = Conventions.MakeDataType(memberType);
        }