Search code examples
entity-frameworklinq-to-entities

pagination in entity framerowk


What is the best practice for pagination in Entity framework? Should one hit database twice, one to get total record count and second to get the records? Thanks in advance.


Solution

  • One solution would be to use a LINQ query which uses the Skip and Take operators.

    Example - Page 1:

    someObject.Skip(0).Take(10)
    

    Example - Page 2:

    someObject.Skip(10).Take(10)
    

    Then you can define a page size variable and use that as the parameter to the Take and Skip (page size variable * (page number - 1)) operator.

    And yes, one hit to get the total record count, to get the total number of pages.