使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *? [英] Select TOP 5 * from SomeTable, using Dataview.RowFilter?

查看:23
本文介绍了使用 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 *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆