LINQ Lambda Expression Syntax
In LINQ, Lambda Expression is a function without a name. It makes the syntax short and clear. Though lambda expression is not readable as the LINQ query, it is equally important as the LINQ query, and it converts into lambda internally. The scope of the lambda expression is limited when we use it as an expression. Lambda Expression cannot be used afterward.
The syntax of defining the lambda expression in LINQ is as:
Lambda Expression is dynamic and decides the type at compile time. In the above lambda expression on the left-hand side, we have a bracket (), which contains the input parameter.
The name of the parameter can be anything and ahead of this parameter (=>) is an Equal to (=) followed by a greater than (>) symbol which is used to send or pass the parameter from left to right side, and on the right-hand side we perform the operation using the input parameter which we will pass from the left-hand side parameter.
This whole syntax forms a Lambda Expression.
Here, we are taking an example of commonly used Expression:
X=>x+10Here, x is an input parameter which is followed by => operator, and next to the operator, there is an expression that adds numeric 10 to the input variable (x). Now the output would increment the numeric 10 to x variable, which was the input parameter on the left-hand side of the Expression.
LINQ Lambda Expression Example in C#
Now run the application, and it will display the list of the country, as shown below in the output in the console window.
In the above example, we created a list object (countries) with the names of the country, and we are using the lambda expression to select the countries from list objects (countries).
Here, Select is the property that we use to select from a list. X is the input parameter, which is on the left-hand side of the Expression, followed by => operator. On the right-hand side of the Expression, we have the same input parameter denotes, which we want to display the parameter without performing any operation on it. We are not specifying any condition on it.