Select TOP 5 * from SomeTable using Dataview.RowFilter?

I need to select the last 5 rows from a cached Dataview object, is there a way to do this?

I 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 () returns the cached DataView

thank

+2


a source to share


2 answers


I ran into the exact same example above in this article , but the last post says the provided code does not work.

However , this article has a solution that actually works, so you can use the code here:



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

      

+3


a source


getALL().Take(5);

      



0


a source







All Articles