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 *?
				
        
 
            
        - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 
						
						
						
						
						