C# LinqDataSource - 你能限制返回的记录数量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LinqDataSource - Can you limit the amount of records returned?
提问by Otto
I'd like to use a LinqDataSource
control on a page and limit the amount of records returned. I know if I use code behind I could do something like this:
我想LinqDataSource
在页面上使用控件并限制返回的记录数量。我知道如果我使用背后的代码,我可以做这样的事情:
IEnumerable<int> values = Enumerable.Range(0, 10);
IEnumerable<int> take3 = values.Take(3);
Does anyone know if something like this is possible with a LinqDataSource
control?
有谁知道这样的事情是否可以通过LinqDataSource
控件实现?
[Update]
[更新]
I'm going to use the LinqDataSource
with the ListView
control, nota GridView or Repeater. The LinqDataSource
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.
我将LinqDataSource
与ListView
控件一起使用,而不是GridView 或中继器。该LinqDataSource
向导不提供限制返回记录数的功能。高级选项仅允许您启用删除、插入和更新。
回答by lomaxx
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
我知道,如果您将分页转发器或 gridview 与 linqdatasource 一起使用,它将自动优化返回的结果数量,但我也很确定在数据源向导中您可以转到高级选项并将其限制为
SELECT TOP 3 FROM
which should allow you to do what you need
这应该让你做你需要的
回答by Portman
Yesand 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.
不,您不能限制 LinqDataSource 控件内的结果。因为 Linq 使用延迟执行,所以期望表示控件将执行记录集限制。
Yes, you can do this with a ListView control. The trick is to use the DataPagercontrol within the LayoutTemplate, like so:
是的,您可以使用 ListView 控件执行此操作。诀窍是在LayoutTemplate 中使用DataPager控件,如下所示:
<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.
通常,您会在 DataPager 中包含控件,例如 first、last、next 和 previous。但是如果你只是把它清空,那么你只会看到你想要的三个结果。
Hope this helps.
希望这可以帮助。
回答by Ollie
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.
我有同样的问题。我解决这个问题的方法是在 LinqDataSource 上使用 Selecting 事件并手动返回结果。
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);
}
回答by Daniel O
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
您可以将 Linq 查询基于存储过程,该过程仅使用 TOP 语句返回 x 行数。请记住,仅仅因为您可以在 Linq 中执行所有数据库代码并不意味着您应该这样做。另外,您可以告诉 Linq 对存储过程使用与普通表相同的返回类型,因此您的所有绑定仍然有效,并且返回结果将是相同的类型
回答by 10gler
You can put event Selecting of LinqDataSource:
您可以放置 LinqDataSource 的事件选择:
protected void ldsLastEntries_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Arguments.MaximumRows = 10;
}
回答by Abd-elhameed Quraim
protected void DocsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Arguments.MaximumRows = 5;
}