How do I get the last 5 rows of a datatable?

francops henri picture francops henri · Jun 5, 2012 · Viewed 8.3k times · Source

How do I get the last 5 rows of a datatable? I tried something like this:

var Long_bottom = LongSlection.Last(5);

Where LongSlection is a DataRow. But I had an error, any idea?

Answer

V4Vendetta picture V4Vendetta · Jun 5, 2012

Not sure what you have got here

var Long_bottom = LongSlection.Last(5);

Assuming you have a DataTable and you want to get the last 5 rows you can do that via

datatable1.AsEnumerable().Reverse().Take(5);

Take and Skip return the specific number of elments (parameter is int) while which is not the case with Last you get the last element or you need a predicate for checking conditions or checks within the row.