Function Expression (Visual Basic)
Declares the parameters and code that define a function lambda expression.
Function ( [ parameterlist ] ) expression - or - Function ( [ parameterlist ] ) [ statements ] End Function
Optional. A list of local variable names that represent the parameters of this procedure. The parentheses must be present even when the list is empty. See Parameter List (Visual Basic).
Required. A single expression. The type of the expression is the return type of the function.
Required. A list of statements that returns a value by using the Return statement. (See Return Statement (Visual Basic).) The type of the value returned is the return type of the function.
A lambda expression is a function without a name that calculates and returns a value. You can use a lambda expression anywhere you can use a delegate type, except as an argument to RemoveHandler. For more information about delegates, and the use of lambda expressions with delegates, see Delegate Statement and Relaxed Delegate Conversion (Visual Basic).
Lambda Expression Syntax
The syntax of a lambda expression resembles that of a standard function. The differences are as follows:
A lambda expression does not have a name.
Lambda expressions cannot have modifiers, such as Overloads or Overrides.
Lambda expressions do not use an As clause to designate the return type of the function. Instead, the type is inferred from the value that the body of a single-line lambda expression evaluates to, or the return value of a multiline lambda expression. For example, if the body of a single-line lambda expression is Where cust.City = "London", its return type is Boolean.
The body of a single-line lambda expression must be an expression, not a statement. The body can consist of a call to a function procedure, but not a call to a sub procedure.
Either all parameters must have specified data types or all must be inferred.
Optional and Paramarray parameters are not permitted.
Generic parameters are not permitted.
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.
Alternatively, you can declare and run the function at the same time.
Following is an example of a lambda expression that increments its argument and returns the value. The example shows both the single-line and multiline lambda expression syntax for a function. For more examples, see Lambda Expressions (Visual Basic).
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)