Beispiel #1
0
        public Pagination(IPagination <T> source)
        {
            _dataSource = source.ToList();

            //PageNumber = source.PageNumber;
            //PageSize = source.PageSize;
            //TotalItems = source.TotalItems;
            //TotalPages = source.TotalPages;
            //FirstItem = source.FirstItem;
            //LastItem = source.LastItem;
            //HasPreviousPage = source.HasPreviousPage;
            HasNextPage = source.HasNextPage;
            Token       = source.Token;
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderField"></param>
        /// <param name="filters"></param>
        /// <returns></returns>
        public virtual Hashtable GetListByPaging(int startIndex, int pageSize, string orderField = null, bool isASC = true, List <NLite.Dynamic.Filter> filters = null)
        {
            using (DbContext ctx = new DbContext(cfg))
            {
                QueryContext qctx = new QueryContext();
                if (!string.IsNullOrEmpty(orderField))
                {
                    qctx.Property  = orderField.Trim();
                    qctx.SortOrder = isASC ? NLite.SortOrder.Ascending : NLite.SortOrder.Descending;
                }
                qctx.PageIndex = startIndex / pageSize;
                qctx.PageSize  = pageSize;
                qctx.Data      = filters;

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

                Hashtable hashtable = new Hashtable();
                hashtable.Add("totalCount", pagination.TotalRowCount);
                hashtable.Add("datas", pagination.ToList());
                return(hashtable);
            }
        }
Beispiel #3
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);
            }
        }