您的位置:首页 > 其它

ef框架实现分页

2016-07-19 00:00 501 查看
Tools.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
public class Tools
{


public List<TEntity> Page<TEntity>(IQueryable<TEntity> source,int PageNum,int PageIndex) where TEntity :class
{
List<TEntity> list = new List<TEntity>();
int count = PageNum * (PageIndex - 1);
using (db)
{


list=source.Skip<TEntity>(count).Take<TEntity>(PageNum).ToList<TEntity>();

}
return list;
}


}

}
Program.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Console.Write("yang");
using (var db = new MyContext())
{


Tools t = new Tools();
IQueryable<Student> source = db.Students.OrderBy(s => s.Id);
List<Student> list = t.Page<Student>(source, 2, 2);


foreach (var s in list)
{
Console.Write(s.Id);
Console.Write("\t");
Console.Write(s.StudentName);
Console.Write("\n");
}


}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ef page