c# 4.0 - Better way to query a page of data and get total count in entity framework 4.1? -


currently when need run query used w/ paging this:

//setup query (typically more complex) var q = ctx.people.where(p=>p.name.startswith("a"));  //get total result count prior sorting int total = q.count();         //apply sort query q = q.orderby(p => p.name);    q.select(p => new personresult {    name = p.name }.skip(skiprows).take(pagesize).toarray(); 

this works, wondered if possible improve more efficient while still using linq? couldn't think of way combine count w/ data retrieval in single trip db w/o using stored proc.

the following query count , page results in 1 trip database, if check sql in linqpad, you'll see it's not pretty. can imagine more complex query.

var query = ctx.people.where (p => p.name.startswith("a"));  var page = query.orderby (p => p.name)                 .select (p => new personresult { name = p.name } )                           .skip(skiprows).take(pagesize)                 .groupby (p => new { total = query.count() })                 .first();  int total = page.key.total; var people = page.select(p => p); 

for simple query this, use either method (2 trips database, or using groupby in 1 trip) , not notice difference. complex, think stored procedure best solution.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -