Exemplo n.º 1
0
        /// <summary>
        /// 建立 Fuzzy 查询条件
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="q">动态查询条件创建者</param>
        /// <param name="property">属性</param>
        /// <param name="expression">查询表达式(支持:1,2,3 或 1-3;如果不符合前面规则,即认为模糊查询;忽略空格;)</param>
        /// <returns></returns>
        public static IQueryBuilder <T> Fuzzy <T>(this IQueryBuilder <T> q, Expression <Func <T, string> > property, string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(q);
            }
            expression = expression.Trim();
            List <string> valueList = new List <string>();

            string[] splits = expression.Split(',');
            if (splits.Length > 1)
            {
                splits.ForEach(c =>
                {
                    string el = c.Trim();
                    if (el.Length > 0)
                    {
                        valueList.Add(el);
                    }
                });

                return(q.In <T, string>(property, valueList.ToArray()));
            }

            int index_minus = expression.IndexOf('-');

            if (index_minus > 0 && index_minus < expression.Length - 1)
            {
                string left  = expression.Substring(0, index_minus).Trim();
                string right = expression.Substring(index_minus + 1).Trim();
                return(q.Between <T>(property, left, right));
            }

            return(q.Like(property, expression));
        }