The following examples show two ways to create simple lambda expressions. The first uses a Dim to provide a name for the function. To call the function, you send in a value for the parameter.
Dim add1 = Function(num As Integer) num + 1
' The following line prints 6.
Console.WriteLine(add1(5))
Alternatively, you can declare and run the function at the same time.
Console.WriteLine((Function(num As Integer) num + 1)(5))
Lambda expressions underlie many of the query operators in Language-Integrated Query (LINQ), and can be used explicitly in method-based queries. The following example shows a typical LINQ query, followed by the translation of the query into method format.
Dim londonCusts = From cust In db.Customers
Where cust.City = "London"
Select cust
' This query is compiled to the following code:
Dim londonCusts = db.Customers _
.Where(Function(cust) cust.City = "London") _
.Select(Function(cust) cust)
For more information about query methods, see Queries (Visual Basic). For more information about standard query operators, see Standard Query Operators Overview.