Vb.net – Linq over DataTable with .Skip() and .Take() method

datatablelinqvb.net

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.

Best Solution

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