Exemplo n.º 1
0
        private LambdaExpression GetOrderSelectorExpression(SortInstruction sortInstruction)
        {
            if (!_fieldProvider.FieldExists(sortInstruction.FieldName))
            {
                throw new FieldNotFoundException(sortInstruction.FieldName, false);
            }

            IFieldCollator <TItem> fieldCollator = _fieldProvider.GetCollator(sortInstruction.FieldName);

            if (fieldCollator == null)
            {
                throw new FieldOperationNotAllowedException(sortInstruction.FieldName, FieldOperation.Sort);
            }

            try
            {
                ParameterExpression itemPram     = Expression.Parameter(typeof(TItem), "srt");
                LambdaExpression    getterLambda = fieldCollator.GetSortExpression(itemPram);

                if (getterLambda == null)
                {
                    throw new NullReferenceException("No sort expression was defined for this field.");
                }

                return(getterLambda);
            }
            catch (Exception ex)
            {
                throw new FieldCannotFilterException(sortInstruction.FieldName, ex);
            }
        }
        /// <remarks>
        /// Ideas from https://stackoverflow.com/questions/5094489
        /// </remarks>
        private Expression <Func <TItem, bool> > GetFilterPredicateExpression(FilterInstruction filterInstruction)
        {
            if (!_fieldProvider.FieldExists(filterInstruction.FieldName))
            {
                throw new FieldNotFoundException(filterInstruction.FieldName, false);
            }

            IFieldCollator <TItem> fieldCollator = _fieldProvider.GetCollator(filterInstruction.FieldName);

            if (fieldCollator == null)
            {
                throw new FieldOperationNotAllowedException(filterInstruction.FieldName, FieldOperation.Filter);
            }

            try
            {
                ParameterExpression itemPram = Expression.Parameter(typeof(TItem), "fltr");

                Expression filterExpression = fieldCollator.GetFilterExpression(itemPram, filterInstruction.Operator, filterInstruction.ValueString);
                if (filterExpression == null)
                {
                    throw new ArgumentNullException(nameof(filterExpression), "No filter expression was defined for this field.");
                }

                Expression <Func <TItem, bool> > predicateLambda = Expression.Lambda <Func <TItem, bool> >(filterExpression, itemPram);
                return(predicateLambda);
            }
            catch (Exception ex)
            {
                throw new FieldCannotFilterException(filterInstruction.FieldName, ex);
            }
        }