In LINQ, Lambda expression is a function without a name. It makes the syntax short and precise. Though it is not as readable as a LINQ query, it is equally essential as the LINQ query and gets converted to lambda internally. Its scope is limited to where it is used as an expression. We cannot reuse it afterward.
Following is the syntax of defining lambda expressions in LINQ.
Lambda Expression is dynamic and decides the type in compile time. In the above lambda expression on the left-hand side, we have a bracket () containing 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 is used to send or pass the input parameter from left to right side, and on the right-hand side, the operation is performed using the input parameter passed from the left-hand side parameters. This whole syntax forms a
Here we have an example to demonstrate what we have read so far. Let’s say we have an expression like as shown below.
X is an input parameter followed by
=> operator, and next to the operator, there is an expression that adds numeric
10 to the input variable (
X). So the output would increment the numeric
10 to the
X variable, which was the input parameter on the left-hand side of the expression.
The following are examples of using LINQ lambda expressions in c# and vb.net.
Select is the property that we use to select from a list. And
x is the input parameter that is on the left-hand side of the expression followed by
=> operator. On the right-hand side of the expression is the same input parameter which denotes that we want to display the parameter without performing any operation. So we are not specifying any condition on it.
We will get the following result when we run the above LINQ lambda expressions example.
We will create one more example to dig further into the LINQ Lambda Expressions. In the following example, we have an integer array having elements from 1 to 10, and we will display only even elements from the array.
In the above examples, we are trying to display the Even and Odd numbers separately from the array list (numbers). Here, the “Where” clause is used as a method in the list to specify the desired condition, and it is supplied by using the Lambda expression. We name an input parameter on the left-hand side, follow the
=> operator, and specify the condition to select only even numbers. Once the numbers are added into the variable named “evennumber” of type IEnumerable<int>, we loop through the collection using a foreach loop to get the result.
Similarly, we are finding the Odd numbers by modifying the condition in our Lambda Expression and storing the result in the variable named oddnumber of type IEnumerable<int> and looping further to get the desired result.
When we execute the above examples, we will get the following result.
This is how we can use the Lambda Expressions in our applications to store the result in a variable of type IEnumerable<int>, and we can loop through it to display the values based on our requirements.