LinqDataSource - Can you limit the amount of records returned? LinqDataSource - Can you limit the amount of records returned? asp.net asp.net

LinqDataSource - Can you limit the amount of records returned?


I had this same issue. The way I got round this was to use the Selecting event on the LinqDataSource and return the result manually.

e.g.

protected void lnqRecentOrder_Selecting(object sender, LinqDataSourceSelectEventArgs e){    DataClassesDataContext dx = new DataClassesDataContext();    e.Result = (from o in dx.Orders                where o.CustomerID == Int32.Parse(Request.QueryString["CustomerID"])                select o).Take(5);}


Yes and No.

No, you cannot limit the results within the LinqDataSource control. Because Linq uses deferred execution, the expectation is that the presentation control will do the recordset limits.

Yes, you can do this with a ListView control. The trick is to use the DataPager control within the LayoutTemplate, like so:

<LayoutTemplate>  <div id="itemPlaceholder" runat="server" />  <asp:DataPager ID="DataPager1" runat="server" PageSize="3">  </asp:DataPager>            </LayoutTemplate>

Normally, you would include controls inside the DataPager like first, last, next, and previous. But if you just make it empty, then you will only see the three results that you desire.

Hope this helps.


protected void DocsData_Selecting(object sender, LinqDataSourceSelectEventArgs e){    e.Arguments.MaximumRows = 5;}