private static Expression TryRemoveNullCheck(ConditionalExpression node)
        {
            var binaryTest = node.Test as BinaryExpression;

            if (binaryTest == null ||
                !(binaryTest.NodeType == ExpressionType.Equal ||
                  binaryTest.NodeType == ExpressionType.NotEqual))
            {
                return(null);
            }

            var leftConstant       = binaryTest.Left as ConstantExpression;
            var isLeftNullConstant = leftConstant != null && leftConstant.Value == null;

            var rightConstant       = binaryTest.Right as ConstantExpression;
            var isRightNullConstant = rightConstant != null && rightConstant.Value == null;

            if (isLeftNullConstant == isRightNullConstant)
            {
                return(null);
            }

            if (binaryTest.NodeType == ExpressionType.Equal)
            {
                var ifTrueConstant = node.IfTrue as ConstantExpression;
                if (ifTrueConstant == null || ifTrueConstant.Value != null)
                {
                    return(null);
                }
            }
            else
            {
                var ifFalseConstant = node.IfFalse as ConstantExpression;
                if (ifFalseConstant == null || ifFalseConstant.Value != null)
                {
                    return(null);
                }
            }

            var testExpression   = isLeftNullConstant ? binaryTest.Right : binaryTest.Left;
            var resultExpression = binaryTest.NodeType == ExpressionType.Equal ? node.IfFalse : node.IfTrue;

            var nullCheckRemovalTestingVisitor = new NullCheckRemovalTestingVisitor();

            if (nullCheckRemovalTestingVisitor.CanRemoveNullCheck(testExpression, resultExpression))
            {
                return(resultExpression);
            }

            return(null);
        }
        private Expression TryRemoveNullCheck(ConditionalExpression node)
        {
            var binaryTest = node.Test as BinaryExpression;

            if (binaryTest == null ||
                !(binaryTest.NodeType == ExpressionType.Equal ||
                  binaryTest.NodeType == ExpressionType.NotEqual))
            {
                return(null);
            }

            var isLeftNullConstant  = binaryTest.Left.IsNullConstantExpression();
            var isRightNullConstant = binaryTest.Right.IsNullConstantExpression();

            if (isLeftNullConstant == isRightNullConstant)
            {
                return(null);
            }

            if (binaryTest.NodeType == ExpressionType.Equal)
            {
                var ifTrueConstant = node.IfTrue as ConstantExpression;
                if (ifTrueConstant == null ||
                    ifTrueConstant.Value != null)
                {
                    return(null);
                }
            }
            else
            {
                var ifFalseConstant = node.IfFalse as ConstantExpression;
                if (ifFalseConstant == null ||
                    ifFalseConstant.Value != null)
                {
                    return(null);
                }
            }

            var testExpression   = isLeftNullConstant ? binaryTest.Right : binaryTest.Left;
            var resultExpression = binaryTest.NodeType == ExpressionType.Equal ? node.IfFalse : node.IfTrue;

            var nullCheckRemovalTestingVisitor = new NullCheckRemovalTestingVisitor(_queryModelVisitor.QueryCompilationContext);

            return(nullCheckRemovalTestingVisitor.CanRemoveNullCheck(testExpression, resultExpression)
                ? resultExpression
                : null);
        }
示例#3
0
        /// <summary>
        ///     Creates a new instance of <see cref="SqlTranslatingExpressionVisitor" />.
        /// </summary>
        /// <param name="dependencies"> Parameter object containing dependencies for this service. </param>
        /// <param name="queryModelVisitor"> The query model visitor. </param>
        /// <param name="targetSelectExpression"> The target select expression. </param>
        /// <param name="topLevelPredicate"> The top level predicate. </param>
        /// <param name="inProjection"> true if the expression to be translated is a LINQ projection. </param>
        public SqlTranslatingExpressionVisitor(
            [NotNull] SqlTranslatingExpressionVisitorDependencies dependencies,
            [NotNull] RelationalQueryModelVisitor queryModelVisitor,
            [CanBeNull] SelectExpression targetSelectExpression = null,
            [CanBeNull] Expression topLevelPredicate            = null,
            bool inProjection = false)
        {
            Check.NotNull(dependencies, nameof(dependencies));
            Check.NotNull(queryModelVisitor, nameof(queryModelVisitor));

            _typeMappingSource              = dependencies.TypeMappingSource;
            _queryModelVisitor              = queryModelVisitor;
            _targetSelectExpression         = targetSelectExpression;
            _topLevelPredicate              = topLevelPredicate;
            _inProjection                   = inProjection;
            _nullCheckRemovalTestingVisitor = new NullCheckRemovalTestingVisitor(_queryModelVisitor);
            _isTopLevelProjection           = inProjection;
        }
        /// <summary>
        ///     Creates a new instance of <see cref="SqlTranslatingExpressionVisitor" />.
        /// </summary>
        /// <param name="dependencies"> Parameter object containing dependencies for this service. </param>
        /// <param name="queryModelVisitor"> The query model visitor. </param>
        /// <param name="targetSelectExpression"> The target select expression. </param>
        /// <param name="topLevelPredicate"> The top level predicate. </param>
        /// <param name="inProjection"> true if the expression to be translated is a LINQ projection. </param>
        public SqlTranslatingExpressionVisitor(
            [NotNull] SqlTranslatingExpressionVisitorDependencies dependencies,
            [NotNull] RelationalQueryModelVisitor queryModelVisitor,
            [CanBeNull] SelectExpression targetSelectExpression = null,
            [CanBeNull] Expression topLevelPredicate            = null,
            bool inProjection = false)
        {
            Check.NotNull(dependencies, nameof(dependencies));
            Check.NotNull(queryModelVisitor, nameof(queryModelVisitor));

            _compositeExpressionFragmentTranslator = dependencies.CompositeExpressionFragmentTranslator;
            _methodCallTranslator           = dependencies.MethodCallTranslator;
            _memberTranslator               = dependencies.MemberTranslator;
            _relationalTypeMapper           = dependencies.RelationalTypeMapper;
            _queryModelVisitor              = queryModelVisitor;
            _targetSelectExpression         = targetSelectExpression;
            _topLevelPredicate              = topLevelPredicate;
            _inProjection                   = inProjection;
            _nullCheckRemovalTestingVisitor = new NullCheckRemovalTestingVisitor(_queryModelVisitor);
            _isTopLevelProjection           = inProjection;
        }