由于在公司中开发项目使用的是C#开发语言,从以前的Java转到这个还真有点不适应,但是经过一个多月的适应终于能够完成小模块的功能,现在阶段对C#还存在着很多的不知,今天学习了下C#实现分页的方法:
特意讲解下有什么不对的地方请指教:
dynamic:表示将在运行时解析其操作对象,用在这儿表示实际运行返回什么类型就是什么类型,这儿就是我们如果不需要使用用在这儿是为了查询表里面其中的部分分布字段,并不是查询所有的字段。
whereLamddal:lamaddal条件表达式
orderLamaddal:lamaddal排序表达式
static List<dynamic> GetPageList<T, TKey>(Expression<Func<T, dynamic>> selector,Expression<Func<T, bool>> whereLamddal,
Expression<Func<T, TKey>> orderLamaddal,int pageIndex,int pageSize,out int total,bool isAsc)
//dynamic:表示将在运行时解析其操作对象,
//用在这儿表示实际运行返回什么类型就是什么类型
where T :class
{
TMSSD_TestEntities context = new TMSSD_TestEntities();
//返回的总条数
total = context.Set<T>().Where(whereLamddal).Count();
var result = context.Set<T>().Where(whereLamddal);
//isAsc用于判断自定义排序
if (isAsc)
{
result=result.OrderBy(orderLamaddal);
}
else
{
result = result.OrderByDescending(orderLamaddal);
}
return result.Skip((pageIndex-1)*pageSize).Take(pageSize).Select(selector).ToList();
}