How to implement Pagination in an IQuerable in VB.net?

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 242
The below code will do so



<HttpGet> _
<Route("records/EmployeeRecords")> _
Public Function GetDetailEmployee(pageNumber As Integer, take As Integer) As List(Of AccountViewModel)
Dim skip = GetSkip(pageNumber, take)

Return GetEmployeeRecords().OrderBy(Function(o) o.EmpID).Skip(skip).Take(take).ToList()
End Function



The GetSkip function is defined as under


Private Shared Function GetSkip(pageNumber As Integer, take As Integer) As Integer
Dim skip = (pageNumber - 1) * take
Return skip
End Function



And the GetEmployeeRecords() is defined as under



Private Function GetEmployeeRecords() As IQueryable(Of EmployeeViewModel)
Dim empRecords As IQueryable(Of EmployeeViewModel) = Nothing
Using _RepoEmployee As IEmployeeRepository = DataAccessAccount.EmployeeRepository
empRecords = New ViewModelDatabase().Employees
End Using
Return empRecords
End Function



The GetEmployeeRecords() is a function that returns an IQueryable.

If the Page number is 1 and take value is 50 and the total records is say 160, then the Skip value will be 0 and take will be 50.
If the Page number is 2 and take value is 50 and the total records is say 160, then the Skip value will be 50 and take will be 50. This indicates that we are requesting from record number 51 to 100.

Hope this helps

Comments or Responses

Login to post response