I have a Business Layer class that uses LINQ to return IQueryable data to a GridView in the User Layer. I want it to return only a single page of data; however, it is returning the entire query. I have heard that paging should work, but every page turn brings back hundreds or thousands of rows and throws out all but the rows set in pagesize.
Here is the LINQ Query (edited down a bit)
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.OrgName开发者_StackOverflowMatch,
IncumbentNameMatch = (bool)m.IncumbentNameMatch,
});
return matches;
}
And here is the 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>
Here is the code that loads the Gridview:
grdMatches.DataSource = new ScoredMatch().List();
grdMatches.DataBind();
And the code that does the paging:
protected void grdMatches_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdMatches.DataSource = new ScoredMatch().List();
grdMatches.PageIndex = e.NewPageIndex;
grdMatches.DataBind();
}
If you just want the current page you should use Skip
and Take
in your LINQ query to get just the results you want.
Eg:
yourQuery.Skip((currentPage - 1) * pageSize).Take(pageSize).Select(....
This will give you just the items that should be displayed in your result set and nothing else.
Check out the following article that will show you how to set it all up in more detail:
http://www.dbtutorials.com/display/linq-to-sql-paging-cs.aspx
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();
}
精彩评论