Linq over DataTable with .Skip() and .Take() method

11,269

if the DataTable is already coming from somewhere else not LINQ2SQL, then Lazy Loading doesn't come into play.

However you can use LINQ2DataSets to take advantage of the Skip() and Take() extension methods.

You need to add a reference to the assembly: System.Data.DataSetExtensions.dll then you can write your function like this:

Public Shared Function GetDataTable(ByVal PageSize As Integer, ByVal CurrentPagea As Integer) As DataTable    
    Dim dtData As New DataTable = da_Book_Content.GetDataContent()    
    Dim query = dtData.AsEnumerable().Skip(CurrentPage).Take(PageSize)    
    Return query.CopyToDataTable()
End Function
Share:
11,269

Related videos on Youtube

Tola
Author by

Tola

A regular guy who writes code to solve the business problems now and then. A Guitarist wannabe.

Updated on April 17, 2022

Comments

  • Tola
    Tola about 2 years

    I have this function that returns a DataTable :

    Public Shared Function GetDataTable(ByVal PageSize As Integer, ByVal CurrentPagea As Integer) As DataTable
    
        Dim dtData As New DataTable
        dtData = da_Book_Content.GetDataContent()
    
        'TODO : how to do data paging for dtData with Linq 
    
        Return dtData
    
    End Function
    

    On a page, I have DataList to display the data. It works but I want to implement the paging feature. How do I do that with so I be able to use Linq lazy loading feature ?

    Thanks.

  • Tola
    Tola about 15 years
    I used this .Skip((CurrentPage - 1) * PageSize).Take(PageSize).CopyToDataTable Thank a lot.