Пример #1
0
        public static FilterDefinition <T> ToMongodbFilter(QueryModel3 queryModel)
        {
            if (queryModel.QueryUnits != null && queryModel.QueryUnits.Any())
            {
                List <FilterDefinition <T> > unitFilters = new List <FilterDefinition <T> >();
                foreach (var unitItem in queryModel.QueryUnits)
                {
                    var unitFilter = MongodbHelper.HandleQueryUnitModel <T>(unitItem);
                    if (unitFilter != null)
                    {
                        unitFilters.Add(unitFilter);
                    }
                }

                return(MongodbHelper.HandleFilterDefinitions(unitFilters, queryModel.LogicalOperator));
            }
            else
            {
                var filter = Builders <T> .Filter.Empty;
                if (queryModel.QueryModels == null || !queryModel.QueryModels.Any())
                {
                    return(filter);
                }

                List <FilterDefinition <T> > queryModelFilters = new List <FilterDefinition <T> >();
                foreach (var queryModel2Item in queryModel.QueryModels)
                {
                    queryModelFilters.Add(ToMongodbFilter(queryModel2Item));
                }

                return(MongodbHelper.HandleFilterDefinitions(queryModelFilters, queryModel.LogicalOperator));
            }
        }
Пример #2
0
        private static FilterDefinition <T> HandleSimpleExpression(string filterStr, Dictionary <string, QueryUnit> keyValues, Dictionary <string, FilterDefinition <T> > dicFilterDefinition)
        {
            //  1、筛选出操作符:&、|
            Queue <char> qOperator = new Queue <char>();

            //Regex regexOperator = new Regex("[&|]");
            //foreach (Match item in regexOperator.Matches(logicalExpression))
            //{
            //    qOperator.Enqueue(item.Value);
            //}
            foreach (char c in filterStr)
            {
                if (c == '&' || c == '|')
                {
                    qOperator.Enqueue(c);
                }
            }
            //  2、筛选出所有的变量
            Queue <string> qVariable = new Queue <string>();

            string[] tempVariables = filterStr.Replace("&", ",").Replace("|", ",").Split(",");
            foreach (string v in tempVariables)
            {
                qVariable.Enqueue(v);
            }
            //  3、返回结果组装
            FilterDefinition <T> filter = null;

            if (qVariable.Count >= 1)
            {
                string tempV = qVariable.Dequeue();
                filter = dicFilterDefinition.ContainsKey(tempV) ? dicFilterDefinition[tempV] : MongodbHelper.HandleQueryUnitModel <T>(keyValues[tempV]);
                while (qVariable.Count > 0)
                {
                    string rightV       = qVariable.Dequeue();
                    var    tempFilter   = dicFilterDefinition.ContainsKey(rightV) ? dicFilterDefinition[rightV] : MongodbHelper.HandleQueryUnitModel <T>(keyValues[rightV]);
                    char   tempOperator = qOperator.Dequeue();
                    switch (tempOperator)
                    {
                    case '&':
                    {
                        filter &= tempFilter;
                        break;
                    }

                    case '|':
                    {
                        filter |= tempFilter;
                        break;
                    }
                    }
                }
                filter = Builders <T> .Filter.Empty & (filter);
            }
            return(filter ?? Builders <T> .Filter.Empty);
        }