[EF]分页处理

之前实现数据真分页,采用ADO.NET的方式,通常写一存储过程实现:


ALTER procedure [dbo].[UP_SqlDataPaging]
@tbName varchar(255),        --表名
@tbFields varchar(1000),      --返回字段
@OrderField varchar(255),   --排序的字段名
@PageSize int,                 --页尺寸
@PageIndex int,                --页码
@OrderType bit,                --排序类型,1是升序,0是降序
@strWhere varchar(1500),    --查询条件
@Total int output            --返回总记录数
as
declare @strSql varchar(5000)     --主语句
declare @strOrder varchar(200)    --排序
declare @strSqlCount nvarchar(500)--查询记录总数主语句
declare @strtemp varchar(50)      --排序附加语句
--------------排序-----------------1是降序,0未升序
if @OrderType !=0
begin
set @strtemp= '>(select max('
set @strOrder='order by ' + @OrderField + ' asc '
end
else
begin
set @strtemp= '<(select min('
set @strOrder='order by '+ @OrderField + ' desc'
end
--------------总记录数---------------
if @strWhere !=''
begin
set @strSqlCount='Select @TotalCout=count(*) from  ' + @tbName + 'where  '+ @strWhere
end
else
begin
set @strSqlCount='Select @TotalCout=count(*) from  ' + @tbName
end
--------------如果是第一页------------
if @PageIndex =1
begin
if @strWhere !=''
begin
set @strSql = 'Select top' +str(@PageSize)+ ' ' + @tbFields + '  from '+ @tbName +' where ' + @strWhere + ' ' + @strOrder
end
else
begin
set @strSql = 'Select top' +str(@PageSize)+ ' ' + @tbFields + '  from '+ @tbName + ' ' + @strOrder
end
end
else
begin
if @strWhere !=''
begin
set @strSql='Select top ' +str(@PageSize) + ' ' + @tbFields
+' from ' + @tbName + ' where '+@OrderField +' '
+ @strtemp+ ' ' +@OrderField+ ') from (select top'
+str((@PageIndex-1)*@PageSize) + ' '+@OrderField +' from '+@tbName
+ @strWhere+ ' '+ @strOrder +') as tb)'+ @strOrder
end
else
begin
set @strSql='Select top ' + str(@PageSize) + ' ' + @tbFields
+' from ' + @tbName + ' where '+@OrderField + ' '
+ @strtemp+ ' ' +@OrderField+ ') from (select top'
+str((@PageIndex-1)*@PageSize) + ' '+@OrderField +' from '+@tbName
+ ' '+ @strOrder +') as tb)'+ @strOrder
end
end
exec sp_executesql @strSqlCount,N'@TotalCout int output',@Total output
exec(@strSql)

调用此存储过程方式。实现过程过于繁琐。
以下为EF的一个控制台程序测试:
利用Skip和Take方法快速实现分页:

      public static void DoSkipTake() {

            using (var context = new KTStoreModel()) {


                var count = context.Products.Count();
                Console.Write("\n数据总行数:{0}\n", count);

                Console.Write("\n预设分页数据项数: 5");
                int pcount = 5;
                int totalpages = count / 5 + 1;
                Console.Write("\n计算的总页数: {0}\n", totalpages);

                Console.Write("\n请输入您要查询的页数: ");
                int pages = int.Parse(Console.ReadLine());

                Console.WriteLine("\n第 {0} 个分页数据: \n", pages);
                var products = (from product in context.Products
                                orderby product.Id
                                select product).                               
                                Skip((pages - 1) * pcount).
                                Take(pcount);

                foreach(var product in products)
                {
                    Console.WriteLine(" {0}\t{1}", product.Id, product.Name);
                }
                Console.Write("\n输出所有数据项: \n");
                var prds = from product in context.Products
                           orderby product.Id
                           select product;
                foreach (var product in prds)
                {
                    Console.WriteLine(" {0}\t{1}", product.Id, product.Name);
                }


                Console.ReadKey();
            }
        }

[EF]分页处理_第1张图片

你可能感兴趣的:(Linq与EF)