r.r r.r - 10 months ago 51
Vb.net Question

What is the best way to give last elements/items of DB table

In my VB code I am reading all the news from database, there are hundreds of them:

For Each lnews In ltable.Rows
Dim litem = lcontent
litem = litem.Replace("%Headline%", _

I need to get just 3 latest ones. What is the best way to do it?

For i As Integer = 0 To 2
Dim s = ltable.Rows(i)
Next i

Answer Source

I'm not sure about definition of "the best way" here, but you can get last 3 rows from DataTable using LINQ like this :

For Each s In ltable.AsEnumerable().Skip(ltable.Rows.Count() - 3)
    's is a DataRow here

or without LINQ :

Dim count = ltable.Rows.Count()
For i As Integer = count - 4 To count - 1
    Dim s = ltable.Rows(i)
Next i