Pagination in C# Entity Framework

c# entity-framework pagination

Question

Is it possible to get the row count of a complicated Linq query that involves millions of entries and two independent searches that visit the database twice?

I could have a different idea. Create a stored procedure; nevertheless, I'm more proficient with MySQL than MSSQL.

Any better ideas would be much appreciated. In addition, if anybody is aware of Microsoft's plans to include this capability into the entity framework.

1
14
4/13/2012 5:52:42 PM

Popular Answer

I would advise use the Take() function. This may be used to define how many records to get from a List or Linq query. For instance

List<customers> _customers = (from a in db.customers select a).ToList();
var _dataToWebPage = _customers.Take(50);

When a user clicks on page 2, 3, etc., I utilize a similar strategy in an MVC project where I write the _customers list to the session and then use this list for further pagination queries. This prevents several database searches. However, it's probably not a good idea to write your list down during the session if it's really long.

You may combine the Take() and Skip() functions to perform pagination. For instance, to get the data from page 2:

var _dataToWebPage = _customers.Skip(50).Take(50);
10
4/16/2012 10:58:33 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow