Example #1
0
        protected override Expression VisitTypeBinary(TypeBinaryExpression typeBinaryExpression)
        {
            Check.NotNull(typeBinaryExpression, nameof(typeBinaryExpression));

            if (typeBinaryExpression.NodeType == ExpressionType.TypeIs &&
                Visit(typeBinaryExpression.Expression) is EntityProjectionExpression entityProjectionExpression)
            {
                var entityType = entityProjectionExpression.EntityType;
                if (entityType.GetAllBaseTypesInclusive().Any(et => et.ClrType == typeBinaryExpression.TypeOperand))
                {
                    return(SqlExpressionFactory.Constant(true));
                }

                var derivedType = entityType.GetDerivedTypes().SingleOrDefault(et => et.ClrType == typeBinaryExpression.TypeOperand);
                if (derivedType != null)
                {
                    var concreteEntityTypes = derivedType.GetConcreteDerivedTypesInclusive().ToList();
                    var discriminatorColumn = entityProjectionExpression.BindProperty(entityType.GetDiscriminatorProperty());

                    return(concreteEntityTypes.Count == 1
                        ? SqlExpressionFactory.Equal(
                               discriminatorColumn,
                               SqlExpressionFactory.Constant(concreteEntityTypes[0].GetDiscriminatorValue()))
                        : (Expression)SqlExpressionFactory.In(
                               discriminatorColumn,
                               SqlExpressionFactory.Constant(concreteEntityTypes.Select(et => et.GetDiscriminatorValue()).ToList()),
                               negated: false));
                }

                return(SqlExpressionFactory.Constant(false));
            }

            return(null);
        }
            protected override Expression VisitExtension(Expression extensionExpression)
            {
                if (extensionExpression is SelectExpression selectExpression)
                {
                    var newSelectExpression = (SelectExpression)base.VisitExtension(extensionExpression);

                    return(newSelectExpression.Predicate is SqlConstantExpression newSelectPredicateConstant &&
                           !(selectExpression.Predicate is SqlConstantExpression)
                        ? newSelectExpression.Update(
                               newSelectExpression.Projection.ToList(),
                               newSelectExpression.Tables.ToList(),
                               SqlExpressionFactory.Equal(
                                   newSelectPredicateConstant,
                                   SqlExpressionFactory.Constant(true, newSelectPredicateConstant.TypeMapping)),
                               newSelectExpression.GroupBy.ToList(),
                               newSelectExpression.Having,
                               newSelectExpression.Orderings.ToList(),
                               newSelectExpression.Limit,
                               newSelectExpression.Offset,
                               newSelectExpression.IsDistinct,
                               newSelectExpression.Alias)
                        : newSelectExpression);
                }

                return(base.VisitExtension(extensionExpression));
            }
            protected override Expression VisitExtension(Expression extensionExpression)
            {
                if (extensionExpression is SelectExpression selectExpression)
                {
                    var newSelectExpression = (SelectExpression)base.VisitExtension(extensionExpression);

                    // if predicate is optimized to true, we can simply remove it
                    var newPredicate = newSelectExpression.Predicate is SqlConstantExpression newSelectPredicateConstant &&
                                       !(selectExpression.Predicate is SqlConstantExpression)
                        ? (bool)newSelectPredicateConstant.Value
                            ? null
                            : SqlExpressionFactory.Equal(
                        newSelectPredicateConstant,
                        SqlExpressionFactory.Constant(true, newSelectPredicateConstant.TypeMapping))
                        : newSelectExpression.Predicate;

                    var newHaving = newSelectExpression.Having is SqlConstantExpression newSelectHavingConstant &&
                                    !(selectExpression.Having is SqlConstantExpression)
                        ? (bool)newSelectHavingConstant.Value
                            ? null
                            : SqlExpressionFactory.Equal(
                        newSelectHavingConstant,
                        SqlExpressionFactory.Constant(true, newSelectHavingConstant.TypeMapping))
                        : newSelectExpression.Having;

                    return(newPredicate != newSelectExpression.Predicate ||
                           newHaving != newSelectExpression.Having
                        ? newSelectExpression.Update(
                               newSelectExpression.Projection.ToList(),
                               newSelectExpression.Tables.ToList(),
                               newPredicate,
                               newSelectExpression.GroupBy.ToList(),
                               newHaving,
                               newSelectExpression.Orderings.ToList(),
                               newSelectExpression.Limit,
                               newSelectExpression.Offset,
                               newSelectExpression.IsDistinct,
                               newSelectExpression.Alias)
                        : newSelectExpression);
                }

                return(base.VisitExtension(extensionExpression));
            }