Apply function to all list elements using VB.NET

Asked

Viewed 159 times

2

Is there a function/module for an object of type List in VB.NET that is similar to array_map() of PHP?

The idea is to create a new variable with the elements that satisfy a certain condition.

I have a list of objects of the type FtpListItem, and only need files where the modification date is greater than the variable UltimoHorario. Function example:

Function ValidarArquivo(arquivo As FtpListItem)
    If arquivo.Modified.Compare(UltimoHorario) > 0 Then
        Return True
    End If
End Function
  • Show what you want to do. You can probably use the LINQ

  • @bigown LINQ can be used with lists? thought it worked only as database mapping..

  • 1

    @Luciorubens Use LINQ for this. You can even use Lambda Expressions to further simplify the search. For example: Ftplistitem.Where(i => i.Last time >0).

1 answer

3


LINQ is a query language to work with any kind of data collection, since arrays to collections that map databases.

You haven’t posted anything that’s easy to identify what you really want but essentially it should be this:

arquivosFiltrados = listaArquivos.Where(x => x.Modified.Compare(UltimoHorario) > 0)

Or if you prefer to use your function:

arquivosFiltrados = listaArquivos.Where(x => ValidarArquivo(x))

I put in the Github for future reference.

Just a reminder that this list must be IEnumarable<FtpListItem> to work. And its function would need to be modified to have a return on all possible paths, ie it should have a Return False if the individual file does not meet the criteria.

If you have other details I better answer. I am assuming that your comparison is being done as you wish.

Try to give a good study in LINQ, it is very useful for a lot of things, either in the form presented or in the form of query language according to the documentation I Linkei above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.