Transform LINQ IQueryable into a paged IQueryable

2019-04-09 02:42发布

I wanna do something like that

    public IQueryable GetPaged<TSource>(IQueryable<TSource> query, int startIndex, int pageSize)
    {
        return GetSession()
          .Linq<TSource>()
          .UseQuery(query)
          .Take(pageSize)
          .Skip(startIndex);
    }

So you can put any IQuerable statement and "it becomes paged" or it will be paged.

I am using LINQ to NHibernate. I hope you get it, sry for this bad english :o

edit: Maybe my approach is the wrong one, is it?

2条回答
Explosion°爆炸
2楼-- · 2019-04-09 03:01
return query.skip(startIndex).take(pageSize);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-09 03:14

This is copied from working code:

public static class QueryableExtensions
{   
    public static IQueryable<T> Paged<T>(this IQueryable<T> source, int page,
                                                                    int pageSize)
    {
        return source
          .Skip((page - 1) * pageSize)
          .Take(pageSize);
    }
}
查看更多
登录 后发表回答