views:

495

answers:

2

Hi!

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?

A: 
return query.skip(startIndex).take(pageSize);
Paul Creasey
The Problem is that I use LINQ to NHibernate and GetSession().Linq<T>() is of Type IQueryable/IQueryable<T> so this does not help me
Rookian
+2  A: 

This is copied from working code:

public static class QueryableExtensions
{   
    public static IQueryable<T> Paged(this IQueryable<T> source, int page, int pageSize)
    {
        return source
          .Skip((page - 1) * pageSize)
          .Take(pageSize);
    }
}
Paco
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
Rename the variable names and you have what you say.
Paco
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
I use my solution in the repository. I don't understand what you mean. Is C# a new language for you?
Paco
I think you dont get me. I am new to Linq/Linq extension methods. Can you post your repository method?
Rookian
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