Get an ulimo record of a list vb.net

0

Hello everyone I have a variable of Type List (of) where I have saved the id, code, name, etc from a list of clients. What I need is to get the last id of the whole list ... I can do it with for each but I prefer a way where I can locate the last row (by the id field) directly

    
asked by edusito87 12.06.2018 в 01:19
source

2 answers

0

Do something like this:

' Crea un array vacío.
Dim frutas() As String = {}

' Obtiene el último elemento del array, o bien,
' un valor por defecto (probablemente null)
' si el arreglo está vacío
Dim ultimo As String = frutas.LastOrDefault()

' Mostrar el resultado.
MsgBox(IIf(String.IsNullOrEmpty(ultimo),
       "<string vacío o nulo>",
       ultimo))

' El código produce la siguiente salira:
'
' <string vacío o nulo>

It's in the .NET reference documentation .

    
answered by 12.06.2018 в 01:41
0

It would be something like this:

Dim lst As New List(Of T) 
...
...
If lst.Count > 0 Then
 Dim lastItem = lst(lst.Count - 1)
End If

To search for an item for a specific id, you should use Linq

Dim itm = lst.FirstOrDefault(Function(f) f.Id = varId)
    
answered by 12.06.2018 в 01:23