src/Library/NetPro.Dapper/Expressions/DapperExpressionVisitor.cs

(开头部分) 7KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace Acadsoc.Dapper.Expressions
{
    /// <summary>
    ///     This class converts an Expression{Func{TEntity, bool}} into an IPredicate group that can be used with
    ///     DapperExtension's predicate system
    /// </summary>
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
    /// <typeparam name="TPrimaryKey">The type of the primary key.</typeparam>
    /// <seealso cref="System.Linq.Expressions.ExpressionVisitor" />
    internal class DapperExpressionVisitor<TEntity, TPrimaryKey> : ExpressionVisitor where TEntity:class
    {
        private PredicateGroup _pg;
        private Expression _processedProperty;
        private bool _unarySpecified;
        private Stack<PredicateGroup> _predicateGroupStack;
        public PredicateGroup _currentGroup { get; set; }
        public DapperExpressionVisitor()
        {
            Expressions = new HashSet<Expression>();
            _predicateGroupStack = new Stack<PredicateGroup>();
        }

        /// <summary>
        ///     Holds BinaryExpressions
        /// </summary>
        public HashSet<Expression> Expressions { get; }

        public IPredicate Process(Expression exp)
        {
            _pg = new PredicateGroup { Predicates = new List<IPredicate>() };
            _currentGroup = _pg;
            Visit(Evaluator.PartialEval(exp));

            // the 1st expression determines root group operator
            if (Expressions.Any())
            {
                _pg.Operator = Expressions.First().NodeType == ExpressionType.OrElse ? GroupOperator.Or : GroupOperator.And;
            }

            return _pg.Predicates.Count == 1 ? _pg.Predicates[0] : _pg;
        }

        private static Operator DetermineOperator(Expression binaryExpression)
        {
            switch (binaryExpression.NodeType)
            {
                case ExpressionType.Equal:
                    return Operator.Eq;
                case ExpressionType.GreaterThan:
                    return Operator.Gt;
                case ExpressionType.GreaterThanOrEqual:
                    return Operator.Ge;
后面还有 157 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。