Ejemplo n.º 1
0
        /// <summary>
        /// 访问Lambda表达式所有运算符(从句、二元运算符)
        /// </summary>
        /// <param name="node">节点</param>
        /// <returns></returns>
        protected override Expression VisitBinary(BinaryExpression node)
        {
            // 遇到从句将两边条件合并
            BooleanClause clause;

            if (ClauseMapping.TryGetValue(node.NodeType, out clause))
            {
                var leftVisitor = new SimpleVisitor();
                leftVisitor.Visit(node.Left);
                var rightVisitor = new SimpleVisitor();
                rightVisitor.Visit(node.Right);
                _filter = new CombinedQueryFilter(leftVisitor._filter, rightVisitor._filter, clause);
                return(node);
            }
            // 遇到二元运算符则将运算逻辑拆分为独立条件
            BinaryOperation symbol;

            if (OperationMapping.TryGetValue(node.NodeType, out symbol))
            {
                var leftVisitor = new SimpleVisitor();
                leftVisitor.Visit(node.Left);
                var rightVisitor = new SimpleVisitor();
                rightVisitor.Visit(node.Right);
                var x = new SingleQueryFilter();
                x.AddPredicate(leftVisitor._fieldName, symbol, rightVisitor._value);
                _filter = x;
                return(node);
            }
            throw new NotSupportedException($"不支将 Lambda 表达式解析为 {nameof(QueryFilter)},请确保右侧的表达式是一个符合规范的二元表达式。");
        }