Transform LINQ IQueryable into a paged IQueryable using LINQ to NHibernate
Asked Answered
J

2

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?

Jansen answered 9/1, 2010 at 21:27 Comment(0)
S
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);
    }
}
Schooling answered 9/1, 2010 at 22:28 Comment(7)
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)Jansen
Rename the variable names and you have what you say.Schooling
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.Jansen
I use my solution in the repository. I don't understand what you mean. Is C# a new language for you?Schooling
I think you dont get me. I am new to Linq/Linq extension methods. Can you post your repository method?Jansen
I'm not going to explain my whole repository to explain extension methods. Please google it or ask a specific question on stack overflow.Schooling
The above code sample would not compile for me because T was unknown. I had to change it to Paged<T>(...Tallia
L
0
return query.skip(startIndex).take(pageSize);
Lucia answered 9/1, 2010 at 21:31 Comment(1)
The Problem is that I use LINQ to NHibernate and GetSession().Linq<T>() is of Type IQueryable/IQueryable<T> so this does not help meJansen

© 2022 - 2024 — McMap. All rights reserved.