views:

571

answers:

2

I'm using linq to sql to get my data, when I set the page size on my data grid and the user selects page 2, I get a postback and I re-read all the data to show the second page. I suspect there should be a better way of doing this, a way that ends up reading just the data I need to show. I was wondering if there are any code samples...

+2  A: 

You should really be looking at the Skip and Take methods.

See ScottGu's post on LINQ to SQL (Part 3), and search the page for "Paging our Query Results" - this has some nice examples.

Alternatively, if you use the LinqDataSource control, and you're talking to a SQL 2005 or 2008 database, you should get this behaviour automatically. LINQ to SQL (Part 5) covers that.

Zhaph - Ben Duguid
+1  A: 

If you want really ease the database load, take a look at client-side paging...

Tomas Lycken
This really depends on the data. Google couldn't get away with client-side paging on a search that returns a million pages. (Although, it sounds like he's loading the entire set from the DB either way)
JerSchneid
Of course. Removing load from the DB will increase the load on other parts of the project - naturally; the job has to be done somewhere. Cliend-side paging will increase network traffic _once_, and reduce DB load by _a lot_ - but you always have to think about _how much_ the network load is increased, and decide if it's worth it.
Tomas Lycken