/// <summary>
        /// Логический оператор "ИЛИ" для объединения двух выражений фильтрации.
        /// </summary>
        /// <param name="left">Выражение слева.</param>
        /// <param name="right">Выражение справа.</param>
        /// <typeparam name="T">Тип фильтруемых объектов.</typeparam>
        /// <returns>Цепочка из двух фильтров с логическим "ИЛИ" между ними.</returns>
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > left, Expression <Func <T, bool> > right)
        {
            ParameterExpression leftParameter = left.Parameters[0];

            SubstitutionExpressionVisitor visitor = new SubstitutionExpressionVisitor();

            visitor.AddExpression(right.Parameters[0], leftParameter);

            Expression body = Expression.OrElse(left.Body, visitor.Visit(right.Body));

            return(Expression.Lambda <Func <T, bool> >(body, leftParameter));
        }
        public void InitializeServer <TModel, TProperty>(Expression <Func <TModel, TProperty> > expression) where TModel : class
        {
            initialized = true;

            ParameterExpression           parameter         = Expression.Parameter(typeof(object));
            UnaryExpression               modelExpression   = Expression.Convert(parameter, typeof(TModel));
            SubstitutionExpressionVisitor expressionVisitor =
                new SubstitutionExpressionVisitor(expression.Parameters.Single(), modelExpression);
            Expression selectExpression = Expression.Convert(expressionVisitor.Visit(expression.Body), typeof(object));

            SelectExpression = Expression.Lambda <Func <object, object> >(selectExpression, parameter);
        }
Beispiel #3
0
        public static Expression <Func <T2, TResult> > ChangeInputType <T1, T2, TResult>(this Expression <Func <T1, TResult> > expression)
        {
            if (!typeof(T1).IsAssignableFrom(typeof(T2)))
            {
                throw new Exception($"{typeof(T1)} is not assignable from {typeof(T2)}.");
            }
            var beforeParameter = expression.Parameters.Single();
            var afterParameter  = Expression.Parameter(typeof(T2), beforeParameter.Name);
            var visitor         = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);

            return(Expression.Lambda <Func <T2, TResult> >(visitor.Visit(expression.Body), afterParameter));
        }
Beispiel #4
0
        public static LambdaExpression ChangeInputType <T, TResult>(this Expression <Func <T, TResult> > expression, Type newInputType)
        {
            if (!typeof(T).IsAssignableFrom(newInputType))
            {
                throw new Exception($"{typeof(T)} is not assignable from {newInputType}.");
            }
            var beforeParameter = expression.Parameters.Single();
            var afterParameter  = Expression.Parameter(newInputType, beforeParameter.Name);
            var visitor         = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);

            return(Expression.Lambda(visitor.Visit(expression.Body), afterParameter));
        }
        private static LambdaExpression ChangeInputType <T, TResult>(Expression <Func <T, TResult> > expression, Type newInputType)
        {
            if (!typeof(T).GetTypeInfo().IsAssignableFrom(newInputType.GetTypeInfo()))
            {
                throw new InvalidOperationException(string.Format("{0} is not assignable from {1}.", typeof(T), newInputType));
            }

            var beforeParameter = expression.Parameters.Single();
            var afterParameter  = Expression.Parameter(newInputType, beforeParameter.Name);
            var visitor         = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);

            return(Expression.Lambda(visitor.Visit(expression.Body), afterParameter));
        }
Beispiel #6
0
        public void InitializeServer <TModel>(Expression <Func <TModel, bool> > expression) where TModel : class
        {
            initialized = true;

            ParameterExpression           parameter         = Expression.Parameter(typeof(object));
            UnaryExpression               modelExpression   = Expression.Convert(parameter, typeof(TModel));
            SubstitutionExpressionVisitor expressionVisitor =
                new SubstitutionExpressionVisitor(expression.Parameters.Single(), modelExpression);
            Expression whereCondition = expressionVisitor.Visit(expression.Body);

            WhereExpression         = Expression.Lambda <Func <object, bool> >(whereCondition, parameter);
            WhereExpressionCompiled = WhereExpression.Compile();
        }
Beispiel #7
0
        public static Expression <Func <TAfter, TResult> > ChangeInputType <TBefore, TAfter, TResult>(Expression <Func <TBefore, TResult> > context)
            where TAfter : TBefore
        {
            // Implementierung orientiert sich an:
            // http://stackoverflow.com/questions/15212779/convert-expressionfunct1-bool-to-expressionfunct2-bool-dynamically

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var beforeParameter = context.Parameters.Single();
            var afterParameter  = Expression.Parameter(typeof(TAfter), beforeParameter.Name);
            var visitor         = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);

            return(Expression.Lambda <Func <TAfter, TResult> >(visitor.Visit(context.Body), afterParameter));
        }