Search code examples
.netentity-frameworklinqpaginationado.net

Paging Support - ADO.NET Entitry Framework and LINQ


What kind of paging support is offered through ADO.NET EF and LINQ?

What does a "first 10" Select look-like?

A "next 10" Select?


Solution

  • The Take keyword is used to decide how many records are to be fetched. A simple example of the Take keyword is provided below.

    List customers = GetCustomerList();

    var first3Customers = (
                from c in customers
    
                select new {c.CustomerID, c.CustomerName} )
                .Take(4);
    

    Here we are taking the first 4 customer for the list provided.

    we can also use the where clause to narrow down the list first and then take 4 of them.

    var first3Customers = (
                from c in customers
               where c.Region == "Kol"
    
                select new {c.CustomerID, c.CustomerName} )
                .Take(4);
    

    But what if we want to get the the data between 4th and 8th record. In this case we use the skip keyword to skip the number of records(from top) we don’t want. Here is the example of using the Skip keyword.

    var first3Customers = (
                from c in customers
               where c.Region == "Kol"
    
                select new {c.CustomerID, c.CustomerName} )
                .Skip(3).Take(4);
    

    More Here