Example #1
0
        /// <summary>
        /// 根据当前页码、分页大小和查询条件获取数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="orderField">排序字段,默认为null</param>
        /// <param name="orderDirection">排序方式:asc| desc,默认为asc</param>
        /// <param name="condition">
        /// <para>查询条件,默认为null,条件之间是“且”的关系</para>
        /// <para>示例:"Name|=|ab;Description|like|www"</para>
        /// <para>相当于sql的:where Name = 'ab' and Description like '%www%'</para>
        /// </param>
        /// <param name="searchField">全文搜索限定字段,多个字段用英文逗号(,)隔开,默认为null:不限定</param>
        /// <param name="searchText">全文搜索关键字,默认为null</param>
        /// <returns></returns>
        public virtual PagingClass GetDataByPagingAndSearch(
            int pageIndex,
            int pageSize,
            string orderField     = null,
            string orderDirection = null,
            string condition      = null,
            string searchField    = null,
            string searchText     = null
            )
        {
            System.Reflection.PropertyInfo[] PropInfoArr = typeof(T).GetProperties();

            string[] PropNameArr = PropInfoArr.Select(x => x.Name).ToArray();
            if (string.IsNullOrEmpty(orderField) && PropNameArr.Length > 0)
            {
                //将第一个字段作为默认排序字段
                orderField = PropNameArr[0];
            }



            //处理全文搜索的条件
            List <NLite.Dynamic.Filter> filtersList = new List <NLite.Dynamic.Filter>();

            if (!string.IsNullOrEmpty(searchText))
            {
                //如果有限定全文搜索的字段,则对全文搜索进行限定
                if (!string.IsNullOrEmpty(searchField))
                {
                    //用逗号分割限定字段,在属性数组中取这些字段
                    string[] searchFieldArr = searchField.Split(',').Where(x => !string.IsNullOrEmpty(x)).ToArray();
                    PropInfoArr = PropInfoArr.Where(p => searchFieldArr.Contains(p.Name)).ToArray();
                }
                //将多个连续的空格替换为单个空格(包括全角空格)
                searchText = Regex.Replace(searchText, @"[  ]+", " ", RegexOptions.IgnoreCase);
                List <string> SearchTextList = searchText.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
                for (int i = 0; i < SearchTextList.Count; i++)
                {
                    Guid g = Guid.NewGuid();//or条件分组的唯一标识
                    foreach (System.Reflection.PropertyInfo pInfo in PropInfoArr)
                    {
                        NLite.Dynamic.Filter filter = new NLite.Dynamic.Filter();
                        filter.OrGroup = "{" + g.ToString("N") + "}";
                        filter.Field   = pInfo.Name;
                        try
                        {
                            string TypeName = pInfo.PropertyType.FullName.ToLower();
                            //如果是数字类型
                            if (TypeName.Contains("int") ||
                                TypeName.Contains("double") ||
                                TypeName.Contains("float"))
                            {
                                filter.Operation = NLite.Dynamic.OperationType.Equal;
                            }
                            else
                            {
                                filter.Operation = NLite.Dynamic.OperationType.Contains;
                            }

                            //如果是可为空的泛型类型
                            if (pInfo.PropertyType.IsGenericType &&
                                pInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                            {
                                //获取Nullable的基础类型
                                Type NullableType = Nullable.GetUnderlyingType(pInfo.PropertyType);
                                filter.Value = Convert.ChangeType(SearchTextList[i], NullableType);
                            }
                            else
                            {
                                filter.Value = Convert.ChangeType(SearchTextList[i], pInfo.PropertyType);
                            }
                        }
                        catch
                        {
                            continue;
                        }
                        filtersList.Add(filter);
                    }
                }
            }

            if (!string.IsNullOrEmpty(condition))
            {
                filtersList.AddRange(GetNliteFilter(condition));
            }

            using (DbContext ctx = new DbContext(cfg))
            {
                QueryContext qctx = new QueryContext();
                if (!string.IsNullOrEmpty(orderField))
                {
                    qctx.Property  = orderField.Trim();
                    qctx.SortOrder = (!string.IsNullOrEmpty(orderDirection) && orderDirection.ToLower() == "desc")
                        ? NLite.SortOrder.Descending
                        : NLite.SortOrder.Ascending;
                }
                qctx.PageIndex = pageIndex < 1 ? 0 : pageIndex - 1;
                qctx.PageSize  = pageSize;
                qctx.Data      = filtersList;

                IPagination <T> pagination = ctx.Set <T>().ToPagination <T>(qctx);

                PagingClass Paging = new PagingClass();
                Paging.PageIndex      = (int)qctx.PageIndex + 1;
                Paging.PageSize       = pageSize;
                Paging.TotalPageCount = (int)Math.Ceiling((double)pagination.TotalRowCount / pageSize);
                Paging.TotalRowCount  = pagination.TotalRowCount;
                Paging.Data           = pagination.ToList();
                return(Paging);
            }
        }
Example #2
0
        /// <summary>
        /// <para>将字符串转换成Nlite的filter</para>
        /// <para>每个条件之间用英文分号分隔(;)</para>
        /// <para>条件里面,字段名、操作符、值用竖线分隔(|)</para>
        /// </summary>
        /// <param name="inputStr">输入字符串</param>
        /// <returns></returns>
        public List <NLite.Dynamic.Filter> GetNliteFilter(string inputStr)
        {
            List <NLite.Dynamic.Filter> Result = new List <NLite.Dynamic.Filter>();

            if (string.IsNullOrWhiteSpace(inputStr))
            {
                return(Result);
            }

            //拆分每个条件
            string[] Conditions = inputStr.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();

            //拆分条件里面的字段名、操作符和值
            foreach (string cond in Conditions)
            {
                string[] paramArr = cond.Split('|');
                if (paramArr.Length != 3)
                {
                    continue;
                }
                NLite.Dynamic.Filter f = new NLite.Dynamic.Filter();
                f.Field = paramArr[0];
                f.Value = paramArr[2];
                //转换操作符
                switch (paramArr[1].ToLower())
                {
                case "=":
                    f.Operation = NLite.Dynamic.OperationType.Equal;
                    break;

                case "<":
                    f.Operation = NLite.Dynamic.OperationType.Less;
                    break;

                case ">":
                    f.Operation = NLite.Dynamic.OperationType.Greater;
                    break;

                case "<=":
                    f.Operation = NLite.Dynamic.OperationType.LessOrEqual;
                    break;

                case ">=":
                    f.Operation = NLite.Dynamic.OperationType.GreaterOrEqual;
                    break;

                case "like":
                    f.Operation = NLite.Dynamic.OperationType.Contains;
                    f.Value     = paramArr[2].Replace("%", "");
                    break;

                case "not like":
                    f.Operation = NLite.Dynamic.OperationType.NotContains;
                    f.Value     = paramArr[2].Replace("%", "");
                    break;

                default:
                    break;
                }
                Result.Add(f);
            }

            return(Result);
        }