分页DataList控件使用LINQ [英] Datalist paging with linq
问题描述
我要创建一个使用Linqfor数据访问一个页面,我使用DataList控件来显示数据。我如何使用LINQ做数据分页?请阅读以下简单的code:
我通常使用PagedDatasource但这似乎只与一个DataTable工作。
下面是我的LINQ到返回的DataTable这势必与Datalist中:
公共共享功能GetStudentList()作为数据表
昏暗的分贝作为新DemoDataClassesDataContext() 昏暗的查询=从圣在db.students _
顺序按升序st.st_studentid _
ST选择 昏暗dtStudent =新的DataTable(神秘岛)
dtStudent.Columns.Add(st_id的GetType(整数))
dtStudent.Columns.Add(st_userid的GetType(GUID))
dtStudent.Columns.Add(st_studentid的GetType(整数))
dtStudent.Columns.Add(st_firstname的GetType(字符串))
dtStudent.Columns.Add(st_lastname的GetType(字符串))
dtStudent.Columns.Add(st_gender的GetType(字符串))
dtStudent.Columns.Add(st_email的GetType(字符串))
对于每一个Q查询
dtStudent.Rows.Add(新的对象(){q.st_id,q.st_userid,q.st_studentid,q.st_firstname,q.st_lastname,q.st_gender,q.st_email})
下一个 返回dtStudent结束功能
在页面背后的code:
保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load 如果没有Page.IsPostBack()然后
LoadData()
万一结束小组私人小组LoadData()
dsStduent = da_Student.GetStudentList()
dt_Student.DataSource = dsStduent
dt_Student.DataBind()结束小组
您会发现方法的跳过()和。取()非常有用的。
我注意到您从您的项目提供了一些code,所以这里的你应该如何实现这些方法的更新。
在您的获取数据的方法,做到以下几点:
暗淡查询=(由ST中db.students _
顺序按升序st.st_studentid _
选择ST).Skip((当前页 - 1)*每页)。取(每页)
然后提供当前页
和每页
变量作为参数传递给方法。 (你不希望将其纳入数据访问,因为他们可以在您网站的不同部位而异...)
I'm creating a page that uses Linqfor data access and I'm using DataList to display data. How can I use Linq to do data paging ? Please read simple code below :
I normally use a PagedDatasource but this only seems to work with a DataTable.
Here's my Linq to return Datatable which is bound with Datalist :
Public Shared Function GetStudentList() As 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
In the code behind of the page :
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
You will find the methods .Skip() and .Take() very useful.
I noticed you provided some code from your project, so here's an update on how you should implement these methods.
In your method for getting the data, do the following:
Dim query = (From st In db.students _
Order By st.st_studentid Ascending _
Select st).Skip((CurrentPage - 1) * PageSize).Take(PageSize)
Then provide the CurrentPage
and PageSize
variables as arguments to the method. (You don't want to build them into the data access, as they could vary across different parts of your site...)
这篇关于分页DataList控件使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!