当前位置: 首页>>代码示例>>C#>>正文


C# BinaryExpression.IsLogicalOperation方法代码示例

本文整理汇总了C#中System.Linq.Expressions.BinaryExpression.IsLogicalOperation方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryExpression.IsLogicalOperation方法的具体用法?C# BinaryExpression.IsLogicalOperation怎么用?C# BinaryExpression.IsLogicalOperation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Linq.Expressions.BinaryExpression的用法示例。


在下文中一共展示了BinaryExpression.IsLogicalOperation方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitBinary

        protected override Expression VisitBinary(BinaryExpression node)
        {
            if (node.IsLogicalOperation())
            {
                var newLeft = Visit(node.Left);
                var newRight = Visit(node.Right);
                var constantLeft = newLeft as ConstantExpression;
                var constantRight = newRight as ConstantExpression;

                if (node.NodeType == ExpressionType.AndAlso)
                {
                    if ((constantLeft != null)
                        && (constantLeft.Type == typeof(bool)))
                    {
                        // true && a => a
                        // false && a => false
                        return (bool)constantLeft.Value ? newRight : newLeft;
                    }

                    if ((constantRight != null)
                       && (constantRight.Type == typeof(bool)))
                    {
                        // a && true => a
                        // a && false => false
                        return (bool)constantRight.Value ? newLeft : newRight;
                    }
                }

                if (node.NodeType == ExpressionType.OrElse)
                {
                    if ((constantLeft != null)
                        && (constantLeft.Type == typeof(bool)))
                    {
                        // true || a => true
                        // false || a => a
                        return (bool)constantLeft.Value ? newLeft : newRight;
                    }

                    if ((constantRight != null)
                       && (constantRight.Type == typeof(bool)))
                    {
                        // a || true => true
                        // a || false => a
                        return (bool)constantRight.Value ? newRight : newLeft;
                    }
                }
            }

            return base.VisitBinary(node);
        }
开发者ID:ChuYuzhi,项目名称:EntityFramework,代码行数:50,代码来源:PredicateReductionExpressionOptimizer.cs

示例2: VisitBinary

            protected override Expression VisitBinary(BinaryExpression node)
            {
                if (!_insideConditionalTest
                    && (node.IsComparisonOperation()
                        || node.IsLogicalOperation()))
                {
                    return Expression.Condition(
                        node,
                        Expression.Constant(true, typeof(bool)),
                        Expression.Constant(false, typeof(bool)));
                }

                return base.VisitBinary(node);
            }
开发者ID:RickyLin,项目名称:EntityFramework,代码行数:14,代码来源:SqlServerQuerySqlGenerator.cs

示例3: VisitBinary

        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used 
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override Expression VisitBinary(BinaryExpression node)
        {
            if (node.IsLogicalOperation())
            {
                var newLeft = Visit(node.Left);
                var newRight = Visit(node.Right);
                var constantLeft = newLeft as ConstantExpression;
                var constantRight = newRight as ConstantExpression;

                if (node.NodeType == ExpressionType.AndAlso)
                {
                    if ((constantLeft != null)
                        && (constantLeft.Type == typeof(bool)))
                    {
                        // true && a => a
                        // false && a => false
                        return (bool)constantLeft.Value ? newRight : newLeft;
                    }

                    if ((constantRight != null)
                        && (constantRight.Type == typeof(bool)))
                    {
                        // a && true => a
                        // a && false => false
                        return (bool)constantRight.Value ? newLeft : newRight;
                    }
                }

                if (node.NodeType == ExpressionType.OrElse)
                {
                    if ((constantLeft != null)
                        && (constantLeft.Type == typeof(bool)))
                    {
                        // true || a => true
                        // false || a => a
                        return (bool)constantLeft.Value ? newLeft : newRight;
                    }

                    if ((constantRight != null)
                        && (constantRight.Type == typeof(bool)))
                    {
                        // a || true => true
                        // a || false => a
                        return (bool)constantRight.Value ? newRight : newLeft;
                    }
                }
            }

            // a == true -> a
            if (node.NodeType == ExpressionType.Equal
                && node.Left.Type.UnwrapNullableType() == typeof(bool)
                && node.Right.Type.UnwrapNullableType() == typeof(bool))
            {
                var newLeft = Visit(node.Left);
                var newRight = Visit(node.Right);

                var leftConstant = newLeft as ConstantExpression;
                if (leftConstant != null && (bool?)leftConstant.Value == true)
                {
                    return newRight.Type == typeof(bool) ? newRight : Expression.Convert(newRight, typeof(bool));
                }

                var rightConstant = newRight as ConstantExpression;
                if (rightConstant != null && (bool?)rightConstant.Value == true)
                {
                    return newLeft.Type == typeof(bool) ? newLeft : Expression.Convert(newLeft, typeof(bool));
                }
            }

            return base.VisitBinary(node);
        }
开发者ID:ymd1223,项目名称:EntityFramework,代码行数:75,代码来源:PredicateReductionExpressionOptimizer.cs


注:本文中的System.Linq.Expressions.BinaryExpression.IsLogicalOperation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。