示例#1
0
            /// <summary>
            /// Translates a given predicate for a given related type.
            /// </summary>
            /// <typeparam name="TTranslatedSource">The type of the translated predicate's parameter.</typeparam>
            /// <param name="path">The path from the desired type to the given type.</param>
            /// <returns>A translated predicate expression.</returns>
            public Expression <Func <TTranslatedSource, bool> > To <TTranslatedSource>(Expression <Func <TTranslatedSource, TSource> > path)
            {
                if (path == null)
                {
                    throw new ArgumentNullException(nameof(path));
                }

                var s = _predicate.Parameters[0];
                var t = path.Parameters[0];

                var binder = new ReplaceExpressionVisitor(s, path.Body);

                return(Expression.Lambda <Func <TTranslatedSource, bool> >(
                           binder.Visit(_predicate.Body), t));
            }
示例#2
0
            /// <summary>
            /// Translates a given predicate for a given related type.
            /// </summary>
            /// <typeparam name="TTranslatedSource">The type of the translated predicate's parameter.</typeparam>
            /// <param name="translation">The translation from the desired type to the given type,
            /// using the initially given predicate to be injected into a new predicate.</param>
            /// <returns>A translated predicate expression.</returns>
            public Expression <Func <TTranslatedSource, bool> > To <TTranslatedSource>(Expression <Func <TTranslatedSource, Func <TSource, bool>, bool> > translation)
            {
                if (translation == null)
                {
                    throw new ArgumentNullException(nameof(translation));
                }

                var t = translation.Parameters[0];
                var s = translation.Parameters[1];

                var binder = new ReplaceExpressionVisitor(s, _predicate);

                return(Expression.Lambda <Func <TTranslatedSource, bool> >(
                           binder.Visit(translation.Body), t));
            }
示例#3
0
        private static Expression <Func <T, bool> > AndAlso <T>(
            this Expression <Func <T, bool> > expr1,
            Expression <Func <T, bool> > expr2,
            Func <Expression, Expression, BinaryExpression> func)
        {
            var parameter = Expression.Parameter(typeof(T));

            var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);
            var left        = leftVisitor.Visit(expr1.Body);

            var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);
            var right        = rightVisitor.Visit(expr2.Body);

            return(Expression.Lambda <Func <T, bool> >(
                       func(left, right), parameter));
        }
示例#4
0
        public static Expression Replace(this Expression baseExpression, Expression search, Expression replace)
        {
            var visitor = new ReplaceExpressionVisitor(search, replace);

            return(visitor.Visit(baseExpression));
        }