r.r r.r - 4 days ago 6
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%", _
Web.HttpUtility.HtmlEncode(lnews("Headline")))
Next


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

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
Next

or without LINQ :

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