使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?

Select TOP 5 * from SomeTable, using Dataview.RowFilter?(使用 Dataview.RowFilter 从 SomeTable 中选择 TOP 5 *?)
本文介绍了使用 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 *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)