<p>I'd like to use a <code>LinqDataSource</code> control on a page and limit the amount of records returned.  I know if I use code behind I could do something like this:</p>

<pre><code>IEnumerable&lt;int&gt; values = Enumerable.Range(0, 10);
IEnumerable&lt;int&gt; take3 = values.Take(3);
</code></pre>

<p>Does anyone know if something like this is possible with a <code>LinqDataSource</code> control?</p>

<p><strong>[Update]</strong></p>

<p>I'm going to use the <code>LinqDataSource</code> with the <code>ListView</code> control, <em>not</em> a GridView or Repeater.  The <code>LinqDataSource</code> wizard does not provide the ability to limit the number of records return.  The Advanced options only allow you to enabled deletes, inserts, and updates.</p>

[{'answer_id': 4231, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 2, 'selected': False, 'text': "<p>I know that if you use a paging repeater or gridview with the linqdatasource it will automatically optimize the number of results returned, but I'm also pretty sure in the datasource wizard you can go to advanced options and limit it to </p>\n\n<pre><code>SELECT TOP 3 FROM \n</code></pre>\n\n<p>which should allow you to do what you need</p>\n"}, {'answer_id': 27327, 'author': 'Portman', 'author_id': 1690, 'author_profile': 'https://Stackoverflow.com/users/1690', 'pm_score': 4, 'selected': False, 'text': '<p><strong>Yes</strong> and <strong>No</strong>.</p>\n\n<p><strong>No</strong>, 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.</p>\n\n<p><strong>Yes</strong>, you can do this with a ListView control. The trick is to use the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.datapager" rel="nofollow noreferrer">DataPager</a> control within the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview.layouttemplate" rel="nofollow noreferrer">LayoutTemplate</a>, like so:</p>\n\n<pre><code>&lt;LayoutTemplate&gt;\n  &lt;div id="itemPlaceholder" runat="server" /&gt;\n  &lt;asp:DataPager ID="DataPager1" runat="server" PageSize="3"&gt;\n  &lt;/asp:DataPager&gt;            \n&lt;/LayoutTemplate&gt;\n</code></pre>\n\n<p>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.</p>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 62158, 'author': 'Ollie', 'author_id': 4453, 'author_profile': 'https://Stackoverflow.com/users/4453', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n\n<p>e.g.</p>\n\n<pre><code>protected void lnqRecentOrder_Selecting(object sender, LinqDataSourceSelectEventArgs e)\n{\n    DataClassesDataContext dx = new DataClassesDataContext();\n    e.Result = (from o in dx.Orders\n                where o.CustomerID == Int32.Parse(Request.QueryString["CustomerID"])\n                select o).Take(5);\n}\n</code></pre>\n'}, {'answer_id': 62196, 'author': 'Daniel O', 'author_id': 697, 'author_profile': 'https://Stackoverflow.com/users/697', 'pm_score': 2, 'selected': False, 'text': "<p>You could base your Linq query on a stored proc that only returns x number of rows using a TOP statement. Remember just because you can do all your DB code in Linq doesn't mean you should. Plus, you can tell Linq to use the same return type for the stored proc as the normal table, so all your binding will still work, and the return results will be the same type</p>\n"}, {'answer_id': 5330890, 'author': '10gler', 'author_id': 663178, 'author_profile': 'https://Stackoverflow.com/users/663178', 'pm_score': 2, 'selected': False, 'text': '<p>You can put event Selecting of LinqDataSource:</p>\n\n<pre><code>protected void ldsLastEntries_Selecting(object sender, LinqDataSourceSelectEventArgs e)\n{\n    e.Arguments.MaximumRows = 10;\n}\n</code></pre>\n'}, {'answer_id': 13048430, 'author': 'Abd-elhameed Quraim', 'author_id': 1771068, 'author_profile': 'https://Stackoverflow.com/users/1771068', 'pm_score': 3, 'selected': False, 'text': '<pre><code>protected void DocsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)\n{\n    e.Arguments.MaximumRows = 5;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/4221', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/519/']