6

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?

Rookian
  • 19,841
  • 28
  • 110
  • 180

2 Answers2

16

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);
    }
}
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
Paco
  • 8,335
  • 3
  • 30
  • 41
  • sry this is not what I wanted. I want to create an IQuerable object. This object shall be passed into the method GetAllPaged(IQuerable queryThatShallbePaged, int pageSize, int startIndex) – Rookian Jan 10 '10 at 11:29
  • Rename the variable names and you have what you say. – Paco Jan 10 '10 at 13:22
  • your solution is used by the client and the client has not to put the query in the method, because of the Linq Extension Method. I wanted to have a solution for my repositories. But yes your solution works, but it is not exactly what I want. I dont want this ... IQueryable.Paged(page, size) I want this ... Paged(query, page, size). But I am not sure what solution is better. – Rookian Jan 10 '10 at 16:07
  • 1
    I use my solution in the repository. I don't understand what you mean. Is C# a new language for you? – Paco Jan 10 '10 at 16:27
  • I think you dont get me. I am new to Linq/Linq extension methods. Can you post your repository method? – Rookian Jan 10 '10 at 17:11
  • I'm not going to explain my whole repository to explain extension methods. Please google it or ask a specific question on stack overflow. – Paco Jan 10 '10 at 17:15
  • 1
    The above code sample would not compile for me because T was unknown. I had to change it to Paged(... – Rob Kent Dec 20 '10 at 11:02
0
return query.skip(startIndex).take(pageSize);
Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
  • 1
    The Problem is that I use LINQ to NHibernate and GetSession().Linq() is of Type IQueryable/IQueryable so this does not help me – Rookian Jan 09 '10 at 21:38