Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pageOfItems"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IPageOfItems <object> ToObjectsPageOfItems <T>(this IPageOfItems <T> pageOfItems)
        {
            var result = new PageOfItems <object>(pageOfItems.Select(x => x as object))
            {
                PageIndex          = pageOfItems.PageIndex,
                PageSize           = pageOfItems.PageSize,
                TotalItemCount     = pageOfItems.TotalItemCount,
                SearchHitViewState = pageOfItems.SearchHitViewState
            };

            return(result);
        }
        /// <summary>
        /// 获取分页列表,IEnumerable从0开始计数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="totalCount"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static IPageOfItems <T> GetPaged <T>(this IEnumerable <T> queryable, int totalCount, int pageIndex, int pageSize)
        {
            //初始化页面信息
            var pageOfItems = new PageOfItems <T>
            {
                PageIndex      = pageIndex,
                PageSize       = pageSize,
                TotalItemCount = totalCount
            };

            pageOfItems.AddRange((queryable.Skip(pageOfItems.StartIndex).Take(pageOfItems.PageSize).ToReadOnlyCollection()).ToList());
            return(pageOfItems);
        }
        /// <summary>
        /// 将类型T的分页数据列表转换为类型TView的分页数据列表
        /// </summary>
        /// <typeparam name="T">原始数据类型</typeparam>
        /// <typeparam name="TView">返回的数据类型</typeparam>
        /// <param name="items">数据列表</param>
        /// <param name="selector">数据转换选择器</param>
        /// <returns>返回TView的分页数据列表</returns>
        public static IPageOfItems <TView> ToView <T, TView>(this IPageOfItems <T> items, Func <T, TView> selector)
        {
            //初始化页面信息
            var pageOfItems = new PageOfItems <TView>
            {
                PageIndex      = items.PageIndex,
                PageSize       = items.PageSize,
                TotalItemCount = items.TotalItemCount,
            };
            var list = items.Select(selector);

            pageOfItems.AddRange(list.ToList());
            return(pageOfItems);
        }