我有一个Business Layer类,它使用LINQ将IQueryable数据返回到用户层中的GridView。我希望它只返回一页数据;但是,它返回整个查询。我听说分页应该可以工作,但是每个页面都会返回数百或数千行,并抛出除pagesize中设置的所有行。
这是LINQ查询(稍微编辑一下)
public IQueryable<ScoredMatch> List()
{
var dc = new PAQcDataLayerDataContext();
var matches = (
from m in dc.ScoredRecordMatches
join c in dc.Customers on m.CustomerId equals c.CustomerId
orderby m.PAQNumber
select new ScoredMatch()
{
Id = m.Id,
PAQId = (int)m.PAQId,
PAQVersion = (int)m.PAQVersion,
JobTitleMatch = (bool)m.JobTitleMatch,
OrgNameMatch = (bool)m.OrgNameMatch,
IncumbentNameMatch = (bool)m.IncumbentNameMatch,
});
return matches;
}
这是GridView:
<asp:GridView ID="grdMatches" runat="server" AutoGenerateColumns="false" CssClass="gridview"
AlternatingRowStyle-CssClass="even" AllowPaging="true" AllowSorting="true" PageSize="10"
DataKeyNames="Id" OnPageIndexChanging="grdMatches_PageIndexChanging" OnSorting="grdMatches_Sorting"
AutoGenerateSelectButton="True" OnSelectedIndexChanging="grdMatches_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="PAQNumber" HeaderText="PAQ #" SortExpression="PAQNumber" />
<asp:BoundField DataField="ScoredNumber" HeaderText="Score #" SortExpression="ScoredNumber" />
<asp:BoundField DataField="CustomerId" HeaderText="Cust #" SortExpression="CustomerId" />
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
<asp:BoundField DataField="DOTNumber" HeaderText="DOT #" SortExpression="DOTNumber" />
<asp:CheckBoxField DataField="OrgNameMatch" HeaderText="Org Match" />
<asp:CheckBoxField DataField="JobTitleMatch" HeaderText="Job Match" />
<asp:CheckBoxField DataField="IncumbentNameMatch" HeaderText="Inc Match" />
</Columns>
</asp:GridView>
以下是加载Gridview的代码:
grdMatches.DataSource = new ScoredMatch().List();
grdMatches.DataBind();
执行分页的代码:
protected void grdMatches_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdMatches.DataSource = new ScoredMatch().List();
grdMatches.PageIndex = e.NewPageIndex;
grdMatches.DataBind();
}
答案 0 :(得分:1)
如果您只想要当前页面,则应在LINQ查询中使用Skip
和Take
来获得所需的结果。
例如:
yourQuery.Skip((currentPage - 1) * pageSize).Take(pageSize).Select(....
这将为您提供应该在结果集中显示的项目,而不是其他内容。
查看以下文章,该文章将向您展示如何更详细地设置它:
http://www.dbtutorials.com/display/linq-to-sql-paging-cs.aspx
答案 1 :(得分:0)
protected void grdMatches_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// not sure where to get "pageSize" with a grid view but would assume it is there someplace.
grdMatches.DataSource = new ScoredMatch()
.Skip(e.NewPageIndex * pageSize)
.Take(pageSize)
.List();
grdMatches.PageIndex = e.NewPageIndex;
grdMatches.DataBind();
}