2009-04-18 12 views
5

Sto creando una pagina che utilizza l'accesso ai dati di Linqfor e sto utilizzando DataList per visualizzare i dati. Come posso usare Linq per eseguire il paging dei dati? Leggere il seguente codice semplice:Cercapersone del datalist con linq

Normalmente utilizzo una PagedDatasource ma sembra funzionare solo con un DataTable.

Ecco il mio LINQ per tornare Datatable che è legato con Datalist:

pubblica condivisa GetStudentList() come DataTable

Dim db As New DemoDataClassesDataContext() 

    Dim query = From st In db.students _ 
       Order By st.st_studentid Ascending _ 
       Select st 

    Dim dtStudent = New DataTable("myst") 


    dtStudent.Columns.Add("st_id", GetType(Integer)) 
    dtStudent.Columns.Add("st_userid", GetType(Guid)) 
    dtStudent.Columns.Add("st_studentid", GetType(Integer)) 
    dtStudent.Columns.Add("st_firstname", GetType(String)) 
    dtStudent.Columns.Add("st_lastname", GetType(String)) 
    dtStudent.Columns.Add("st_gender", GetType(String)) 
    dtStudent.Columns.Add("st_email", GetType(String)) 


    For Each q In query 
     dtStudent.Rows.Add(New Object() {q.st_id, q.st_userid, q.st_studentid, q.st_firstname, q.st_lastname, q.st_gender, q.st_email}) 
    Next 

    Return dtStudent 

End Function 

Nel codice dietro della pagina:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    If Not Page.IsPostBack() Then 
     LoadData() 
    End If 

End Sub 

Private Sub LoadData() 
    dsStduent = da_Student.GetStudentList() 
    dt_Student.DataSource = dsStduent 
    dt_Student.DataBind() 

End Sub 

risposta

8

Troverete i metodi .Skip() and .Take() molto utili.

Ho notato che hai fornito del codice dal tuo progetto, quindi ecco un aggiornamento su come implementare questi metodi.

nel metodo per ottenere i dati, effettuare le seguenti operazioni:

Dim query = (From st In db.students _ 
      Order By st.st_studentid Ascending _ 
      Select st).Skip((CurrentPage - 1) * PageSize).Take(PageSize) 

quindi fornire le variabili CurrentPage e PageSize come argomenti al metodo. (Non vuoi costruirli nell'accesso ai dati, poiché potrebbero variare tra diverse parti del tuo sito ...)

+0

Come identificare se i dati hanno raggiunto il limite massimo, ovvero siamo nell'ultima pagina? –

+0

Lavorato attraverso alcune combinazioni. Ora sta funzionando per me. Grazie per la tua risposta :) –

Problemi correlati