user240141
user240141

Reputation:

Paging in Gridview with Linq

I know my types of various other issues are listed here in SO, but I think my issue is quite different from the existing one or mine approach is different.

I have a table call tblListing where there is 108 columns, in which i have 170000 records and increasing. In my admin section i am pulling out 20 columns. But the problem is the its taking more than a minute to display in gridview.

Earlier , i was using datatable to display the records, but the page and system became very sluggish. Now i applied LINQ, and is using .Take(20),but its displaying only 20 records.

So I want to load 20 records at a time, while paging.

Please help. Any sample code or refence will be highly appreciated.

Upvotes: 1

Views: 1344

Answers (1)

Jeff Lauder
Jeff Lauder

Reputation: 1247

Looks like this article about Paging With LINQ may be helpful for you:

Here's the relevant code:

public static class PagingExtensions
{
    //used by LINQ to SQL
    public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
    {
        return source.Skip((page - 1) * pageSize).Take(pageSize);
    }

    //used by LINQ
    public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
    {
        return source.Skip((page - 1) * pageSize).Take(pageSize);
    }
}

Upvotes: 3

Related Questions