Linq分页

From:http://oct01.cn/all.aspx?id=25

var query = from oneItem in TableName
           
select oneItem;

//number为一页显示的个数,i为第i页
List<TableItem> GetItemsInOnePage(int number, int i)
{
   
return query.Take(number * i).Skip(number * (i - 1)).ToList();
}

 

namespace Test
{
   
public class LinqTest
    {
       
//连接字符串,需加上密码
        private string connection = "Data Source=localhost;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=";

 

 

//TestDBDataContext 为通过dbml文件生成的数据库类
        private TestDBDataContext GetDB()
        {
            TestDBDataContext db
= new TestDBDataContext(connection);
           
return db;
        }

 

 

public int GetQueryPagesCount(int number)
        {
           
var db = GetDB();
           
var query = from oneItem in db.Table1s
                       
select oneItem;
           
return (query.Count() / number + 1);//不足一页按一页算
        }

 

 

//number为一页显示的个数,i为第i页
        public List<Table1> QueryByPage(int number, int i)
        {
           
var query = from oneItem in db.Table1s
                       
select oneItem;
           
return query.Take(number * i).Skip(number * (i - 1)).ToList();
        }
    }
}

posted @ 2010-07-01 13:36  山村果园  阅读(265)  评论(0编辑  收藏  举报