Esempio n. 1
0
        protected override Expression VisitLike(QueryTranslatorContext context, LikeExpression expression)
        {
            // Is the left side a collection?
            if (expression.Left is CollectionExpression collection)
            {
                // Build the collection accessor parameter.
                var parameter = collection.Accept(this, context);

                // Build the predicate expression.
                context.PushParameter(collection.ItemType);
                var predicate = Expression.Lambda(
                    new LikeExpression(
                        collection.ItemAccessor,
                        expression.Right).Accept(this, context),
                    context.Parameter);
                context.PopParameter();

                // We need to use an Enumerable.Any together with the LIKE predicate.
                return(Expression.Call(typeof(Enumerable), "Any", new Type[] { collection.ItemType }, parameter, predicate));
            }

            var left  = expression.Left.Accept(this, context);
            var right = expression.Right.Accept(this, context);

            return(Expression.Call(
                       null, typeof(DbFunctionsExtensions).GetMethod("Like", new[] { typeof(DbFunctions), typeof(string), typeof(string) }),
                       Expression.Constant(EF.Functions), left, right));
        }
Esempio n. 2
0
        protected override Expression VisitRelation(QueryTranslatorContext context, RelationalExpression expression)
        {
            // Is the left side a collection?
            if (expression.Left is CollectionExpression collection)
            {
                // Build the collection accessor parameter.
                var parameter = collection.Accept(this, context);

                // Build the predicate expression.
                context.PushParameter(collection.ItemType);
                var predicate = Expression.Lambda(
                    new RelationalExpression(
                        collection.ItemAccessor,
                        expression.Right,
                        expression.Operator).Accept(this, context),
                    context.Parameter);
                context.PopParameter();

                // Combine them into an Enumerable.Any or Enumerable.All call.
                var method = expression.Operator == RelationalOperator.NotEqualTo ? "All" : "Any";
                return(Expression.Call(typeof(Enumerable), method, new Type[] { collection.ItemType }, parameter, predicate));
            }

            var left  = expression.Left.Accept(this, context);
            var right = expression.Right.Accept(this, context);

            switch (expression.Operator)
            {
            case RelationalOperator.EqualTo:
                return(Expression.Equal(left, right));

            case RelationalOperator.NotEqualTo:
                return(Expression.NotEqual(left, right));

            case RelationalOperator.GreaterThan:
                return(Expression.GreaterThan(left, right));

            case RelationalOperator.GreaterThanOrEqualTo:
                return(Expression.GreaterThanOrEqual(left, right));

            case RelationalOperator.LessThan:
                return(Expression.LessThan(left, right));

            case RelationalOperator.LessThanOrEqualTo:
                return(Expression.LessThanOrEqual(left, right));
            }

            throw new InvalidOperationException($"Unknown operator '{expression.Operator}'.");
        }