Beispiel #1
0
        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($"不支将 Lamda 表达式解析为 {nameof(QueryFilter)},请确保右侧的表达式是一个符合规范的二元表达式。");
        }
        private string GenerateSingleFilter <TEntity>(SingleQueryFilter queryFilter, DynamicParameters parameters)
        {
            if (queryFilter.IsEmpty)
            {
                return(String.Empty);
            }
            string spliter = queryFilter.Clause == BooleanClause.Or ? " OR " : " AND ";

            return(queryFilter.Select(p => GeneratePredicateFilter <TEntity>(p, parameters)).ToArray().ToArrayString(spliter));
        }
        /// <summary>
        /// 生成查询过滤。
        /// </summary>
        /// <param name="queryFilter">查询过来器实例。</param>
        /// <param name="parameters">查询参数</param>
        /// <returns></returns>
        public String GenerateFilter <TEntity>(QueryFilter queryFilter, DynamicParameters parameters)
        {
            if (queryFilter != null)
            {
                SingleQueryFilter sqf = queryFilter as SingleQueryFilter;
                if (sqf != null)
                {
                    return(this.GenerateSingleFilter <TEntity>(sqf, parameters));
                }

                CombinedQueryFilter cqf = queryFilter as CombinedQueryFilter;
                if (cqf != null)
                {
                    return(GenerateCombinedFilter <TEntity>(cqf, parameters));
                }
            }
            return(null);
        }
Beispiel #4
0
        public Task <IEnumerable <T> > QueryAsync <TField>(string fieldName, IEnumerable <TField> fieldValues)
        {
            Guard.ArgumentNullOrWhiteSpaceString(fieldName, nameof(fieldName));
            if (fieldValues.IsNullOrEmpty())
            {
                return(Task.FromResult(Enumerable.Empty <T>()));
            }
            if (fieldValues.Count() == 1)
            {
                SingleQueryFilter filter = new SingleQueryFilter();
                filter.AddEqual(fieldName, fieldValues.First());
                return(this.QueryAsync(filter));
            }
            else
            {
                var sql = GenerateQueryInSql(fieldName, fieldValues.Cast <Object>());

                var connection = this.GetReadingConnection();
                return(connection.QueryAsync <T>(sql.Sql, sql.Parameters));
            }
        }