Select TOP 5 * from SomeTable, using Dataview.RowFilter?(使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?)
问题描述
我需要从缓存的 Dataview 对象中选择最近的 5 行,有什么办法吗?
I need to select 5 most recent rows from cached Dataview object, is there any way to do that?
我试过了,但是 Indexer DataColumn 是空的.:
I've tried but Indexer DataColumn is empty. :
public static DataView getLatestFourActive()
{
DataTable productDataTable = getAll().ToTable();
DataColumn ExpressionColumn = new DataColumn("Indexer",typeof(System.Int32));
ExpressionColumn.Unique = true;
ExpressionColumn.AutoIncrement = true;
ExpressionColumn.AllowDBNull = false;
ExpressionColumn.AutoIncrementSeed = 0;
ExpressionColumn.AutoIncrementStep = 1;
productDataTable.Columns.Add(ExpressionColumn);
DataView productFilteredView = productDataTable.DefaultView;
productFilteredView.RowFilter = "isActive=1 and Indexer<4";
return productFilteredView;
}
getAll() 返回缓存的 DataView
getAll() returns cached DataView
谢谢
推荐答案
我在 这篇文章,但最后一篇文章说提供的代码不起作用.
I encountered the same sample above in this article, but the last post says the provided code doesn't work.
但是,这篇文章有一个可行的解决方案,所以这里是您可以使用的代码:
However, this article has a solution that does work, so here's the code you could use:
public static DataView getLatestFourActive() {
DataTable productDataTable = getAll().ToTable();
DataTable cloneDataTable = productDataTable.Clone();
for (int i = 0; i < 4; i++) {
cloneDataTable.ImportRow(productDataTable.Rows[i]);
}
return new DataView(cloneDataTable);
}
这篇关于使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01