Is there any way to pass a lambda expression as an argument to a paramer in a method? For example:
Private Sub Metodo(Expressao As ?)
Lista = Lista.Where(Expressao)
End Sub
Is there any way to pass a lambda expression as an argument to a paramer in a method? For example:
Private Sub Metodo(Expressao As ?)
Lista = Lista.Where(Expressao)
End Sub
The most commonly used way is to use a predefined delegate, such as Func(Of TSource, Boolean)
:
Private Sub Metodo(Of TSource)(Expressao As Func(Of TSource, Boolean))
Lista = Lista.Where(Expressao)
End Sub
TSource
is generic, if you know the specific type you can use it. If it is a Integer
, it can be:
Private Sub Metodo(Expressao As Func(Of Integer, Boolean))
Lista = Lista.Where(Expressao)
End Sub
To call is not as convenient as C #:
Metodo(Function(x) x = 0)
Code:
Public Class Prj
Public Sub Metodo(Of T)(Where As Func(Of T, Boolean))
Lista = Lista.Where(Where)
End Sub
End Class
How to use?
Dim c As New Prj
c.Metodo(Function(a) a = 1);
References