protected DbExistsExpression UpdateExists(DbExistsExpression exists, 
     DbSelectExpression select)
 {
     if (select != exists.Select)
         return new DbExistsExpression(select);
     
     return exists;
 }
 protected virtual bool CompareExists(DbExistsExpression a, DbExistsExpression b)
 {
     return(Compare(a.Select, b.Select));
 }
 protected virtual Expression VisitExists(DbExistsExpression exists)
 {
     DbSelectExpression select = (DbSelectExpression)Visit(exists.Select);
     return UpdateExists(exists, select);
 }
Example #4
0
        private Expression BindAnyAll(Expression source, MethodInfo method, LambdaExpression predicate, bool isRoot)
        {
            bool isAll = method.Name == "All";
            ConstantExpression constSource = source as ConstantExpression;

            if (constSource != null && !IsQuery(constSource))
            {
                System.Diagnostics.Debug.Assert(!isRoot);
                Expression where = null;
                foreach (object value in (IEnumerable)constSource.Value)
                {
                    Expression expr = Expression.Invoke(predicate, Expression.Constant(value, predicate.Parameters[0].Type));
                    if (where == null)
                    {
                        where = expr;
                    }
                    else if (isAll)
                    {
                        where = where.And(expr);
                    }
                    else
                    {
                        where = where.Or(expr);
                    }
                }
                return(this.Visit(where));
            }
            else
            {
                if (isAll)
                {
                    predicate = Expression.Lambda(Expression.Not(predicate.Body), predicate.Parameters.ToArray());
                }
                if (predicate != null)
                {
                    source = Expression.Call(typeof(Queryable), "Where", method.GetGenericArguments(), source, predicate);
                }
                DbProjectionExpression projection = this.VisitSequence(source);
                Expression             result     = new DbExistsExpression(projection.Select);
                if (isAll)
                {
                    result = Expression.Not(result);
                }
                if (isRoot)
                {
                    if (this.mapping.Language.AllowSubqueryInSelectWithoutFrom)
                    {
                        return(GetSingletonSequence(result, "SingleOrDefault"));
                    }
                    else
                    {
                        // use count aggregate instead of exists
                        var newSelect = projection.Select.SetColumns(
                            new[] { new DbColumnDeclaration("value", new DbAggregateExpression(typeof(int), DbAggregateType.Count, null, false)) }
                            );
                        var colx = new DbColumnExpression(typeof(int), null, newSelect.Alias, "value");
                        var exp  = isAll
                            ? colx.Equal(Expression.Constant(0))
                            : colx.GreaterThan(Expression.Constant(0));
                        return(new DbProjectionExpression(
                                   newSelect, exp, Aggregator.Aggregate(typeof(bool), typeof(IEnumerable <bool>))
                                   ));
                    }
                }
                return(result);
            }
        }
 private Expression BindAnyAll(Expression source, MethodInfo method, LambdaExpression predicate, bool isRoot)
 {
     bool isAll = method.Name == "All";
     ConstantExpression constSource = source as ConstantExpression;
     if (constSource != null && !IsQuery(constSource))
     {
         System.Diagnostics.Debug.Assert(!isRoot);
         Expression where = null;
         foreach (object value in (IEnumerable)constSource.Value)
         {
             Expression expr = Expression.Invoke(predicate, Expression.Constant(value, predicate.Parameters[0].Type));
             if (where == null)
             {
                 where = expr;
             }
             else if (isAll)
             {
                 where = where.And(expr);
             }
             else
             {
                 where = where.Or(expr);
             }
         }
         return this.Visit(where);
     }
     else
     {
         if (isAll)
         {
             predicate = Expression.Lambda(Expression.Not(predicate.Body), predicate.Parameters.ToArray());
         }
         if (predicate != null)
         {
             source = Expression.Call(typeof(Queryable), "Where", method.GetGenericArguments(), source, predicate);
         }
         DbProjectionExpression projection = this.VisitSequence(source);
         Expression result = new DbExistsExpression(projection.Select);
         if (isAll)
         {
             result = Expression.Not(result);
         }
         if (isRoot)
         {
             if (this.mapping.Language.AllowSubqueryInSelectWithoutFrom)
             {
                 return GetSingletonSequence(result, "SingleOrDefault");
             }
             else
             {
                 // use count aggregate instead of exists
                 var newSelect = projection.Select.SetColumns(
                     new[] { new DbColumnDeclaration("value", new DbAggregateExpression(typeof(int), DbAggregateType.Count, null, false)) }
                     );
                 var colx = new DbColumnExpression(typeof(int), null, newSelect.Alias, "value");
                 var exp = isAll 
                     ? colx.Equal(Expression.Constant(0))
                     : colx.GreaterThan(Expression.Constant(0));
                 return new DbProjectionExpression(
                     newSelect, exp, Aggregator.Aggregate(typeof(bool), typeof(IEnumerable<bool>))
                     );
             }
         }
         return result;
     }
 }
 protected override Expression VisitExists(DbExistsExpression exists)
 {
     this.Write("EXISTS(");
     this.WriteLine(Indentation.Inner);
     this.Visit(exists.Select);
     this.WriteLine(Indentation.Same);
     this.Write(")");
     this.Indent(Indentation.Outer);
     return exists;
 }
 protected virtual bool CompareExists(DbExistsExpression a, DbExistsExpression b)
 {
     return Compare(a.Select, b.Select);
 }
Example #8
0
        protected virtual Expression VisitExists(DbExistsExpression exists)
        {
            DbSelectExpression select = (DbSelectExpression)Visit(exists.Select);

            return(UpdateExists(exists, select));
        }