LINQ to SQL分页
问题描述:
我们在LINQ to SQL中有orderby子句,就像下面一样,但是我们有什么分页功能吗?LINQ to SQL分页
from trans in DB.transactions
orderby trans.column descending
select trans;
或者我们必须手动(排序)像下面的查询?
(from trans in DB.transactions
orderby trans.column descending
select trans).Skip(noOfRecords).Take(pageSize);
答
据我所知,你必须使用.Skip
和.Take
,但你可以写IQueryable
的扩展,方便您的工作:
public static class Extensions
{
public static IQueryable<T> Paging<T>(this IQueryable<T> source, int pageNumber, int pageLength)
{
return source.Skip(pageNumber * pageLength).Take(pageLength);
}
}
var source = (from trans in DB.transactions
orderby "cancellation_reason_id" descending
select trans);
source = source.Paging(0, 10); // Get first page with 10 item
是。你必须通过“跳过”和“采取”方法来做到这一点。 –