给你个例子,在EF里面最好先建立个视图view,效率高,然后针对view进行分页,排序...
public virtual Permissions GetPermissionsByRoleQueryable(Int32 roleID, Nullable<Int32> page, Int32 pageSize, string orderProperty, bool ASC)
{
IQueryable<Permission> query = GetPermissionsByRole(roleID).AsQueryable<Permission>();
if (false == string.IsNullOrEmpty(orderProperty))
{
switch(orderProperty)
{
case "ID":
if (true == ASC) query = query.OrderBy(P => P.ID);
else query = query.OrderByDescending(P => P.ID);
break;
case "Name":
if (true == ASC) query = query.OrderBy(P => P.Name);
else
query = query.OrderByDescending(P => P.Name);
break;
default:
break;
}
}
if (pageSize > 0)
{
query = query.Skip((page ?? 0) * pageSize).Take(pageSize);
}
return new Permissions(query);
}
其实如果实在不知道,你完全可以先联接查询,然后再进行分页,
比如 var query = from a1 in atable
join b1 in btable on a1.xx equals b1.xx
select new ...;
var result = query.skip(2).take(2);
说到底我也不会。因为在gridview分页的时候我都用系统自带的分页功能。
(from xxxxxxxxxxxxx
select new {字段}).Skip(start).Take(limit);
Linq有两种写法,一种是用LAMDA表达式,一种用标准表达式