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");
}
}
}
}
}
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中使用MySQL的方法及常见问题
- jsp中page指令用法详解
- asp.net Page.EnableEventValidation 属性验证服务器控件的回发和回调事件出现的错误
- JSP编译指令page、include详解
- ThinkPHP使用心得分享-分页类Page的用法
- ThinkPHP CURD方法之page方法详解
- ASP.NET Mvc开发之EF延迟加载
- InnoDB脏页刷新机制Checkpoint
- EntityFramework
- 透过pageinspect了解PostgreSQl中的page
- Mybatis源码概览(二) ---Plugin扩展与Spring结合原理
- 在EF中使用MySQL的方法及常见问题
- double write buffer
- asp.net 页面请求过程
- pagecontext page
- Linux中如何杀掉僵尸进程
- 锁定IE浏览器主面
- 在线man page
- jQuery simplePage+AJAX plus 分页插件
- jsp 的四个作用域 :page、request、session和application的区别 .