本文介绍了选择TOP 5 *从SomeTable,使用Dataview.RowFilter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择5从缓存数据视图对象的最新行,有没有办法做到这一点?

I need to select 5 most recent rows from cached Dataview object, is there any way to do that?

我做过尝试,但索引的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()返回缓存的数据视图

getAll() returns cached DataView

感谢

推荐答案

我遇到了同样的样品上面的,但过去后说,所提供的code不起作用。

I encountered the same sample above in this article, but the last post says the provided code doesn't work.

然而,这篇文章有一个解决方案,它的工作,所以这里的code您可以使用:

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);
}

这篇关于选择TOP 5 *从SomeTable,使用Dataview.RowFilter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:58