Why does LINQ-to-SQL Paging fail inside a function?
        Posted  
        
            by ssg
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ssg
        
        
        
        Published on 2010-03-15T19:50:58Z
        Indexed on 
            2010/03/15
            19:59 UTC
        
        
        Read the original article
        Hit count: 401
        
Here I have an arbitrary IEnumerable<T>. And I'd like to page it using a generic helper function instead of writing Skip/Take pairs every time. Here is my function:
IEnumerable<T> GetPagedResults<T>(IEnumerable<T> query, int pageIndex, int pageSize)
{
   return query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
}
And my code is:
result = GetPagedResults(query, 1, 10).ToList();
This produces a SELECT statement without TOP 10 keyword. But this code below produces the SELECT with it:
result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
What am I doing wrong in the function?
© Stack Overflow or respective owner