LINQ Lambda Expressions

  Total Views : 512
  Comments : 0
 
  Keywords : Linq lambda expression tutorial with examples, Lambda expressions in linq with examples, Lambda expressions in linq queries with examples, What is the lambda expression in linq with example

Here we will learn linq lambda expressions in c#, vb.net with examples and how to use linq lambda expressions in c#, vb.net with examples.

LINQ Lambda Expressions

In LINQ Lambda expression is a function without a name. It makes the syntax short and clear. Though it is not as readable as a LINQ Query but it is equally important as the LINQ query and gets convert to lambda internally. Its scope is limited to where it is used as an expression. We cannot reuse it afterwards.

LINQ Lambda Expressions Syntax

Following is the syntax of defining lambda expressions in LINQ.

 

(Input Parameter) => Method Expression

Lambda Expression is dynamic and decides the type in compile time. In above lambda expression on the left hand side we have a bracket containing 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 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 Lambda Expression.

 

Here we have an example to demonstrate what we have read so far.

 

Let’s say we have an expression like this:

 

X => X +10

Here X is an input parameter followed by => operator. And next to the operator 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.

LINQ Lambda Expressions Example

Now we will create simple application to use lambda expressions.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

List<string> countries = new List<string>();

countries.Add("India");

countries.Add("US");

countries.Add("Australia");

countries.Add("Russia");

IEnumerable<string> result = countries.Select(x => x);

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim countries As New List(Of String)()

countries.Add("India")

countries.Add("US")

countries.Add("Australia")

countries.Add("Russia")

Dim result As IEnumerable(Of String) = countries.[Select](Function(x) x)

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

 In this example we have a List of type string that has countries stored in it. We use lambda expression to select the countries from the List<string> countries. Here 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 just want to display the parameter without performing any operation on it. So we are not specifying any condition on it.

 

On running the above linq lambda expressions example, we will get the output like as shown below

 

India

US

Australia

Russia

We will create one more sample demo to dig further into Lambda Expression. In this example we have an integer array having elements from 1 to 10. We will display even elements from the array.

LINQ Lambda Expressions Example 2

Write the following code console application.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Console.WriteLine("Below are the even numbers");

Console.WriteLine("");

IEnumerable<int> evennumber = numbers.Where(x => x % 2 == 0);

foreach (var item in evennumber)

{

Console.WriteLine(item + " is an even number");

}

Console.WriteLine("--------------------------------------------------");

Console.WriteLine("Below are the Odd numbers");

Console.WriteLine("");

IEnumerable<int> oddnumber = numbers.Where(x => x % 2 != 0);

foreach (var item in oddnumber)

{

Console.WriteLine(item + " is an odd number");

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim numbers As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Console.WriteLine("Below are the even numbers")

Console.WriteLine("")

Dim evennumber As IEnumerable(Of Integer) = numbers.Where(Function(x) x Mod 2 = 0)

For Each item In evennumber

Console.WriteLine("{0} is an even number", item)

Next

Console.WriteLine("--------------------------------------------------")

Console.WriteLine("Below are the Odd numbers")

Console.WriteLine("")

Dim oddnumber As IEnumerable(Of Integer) = numbers.Where(Function(x) x Mod 2 <> 0)

For Each item In oddnumber

Console.WriteLine("{0} is an odd number", item)

Next

Console.ReadLine()

End Sub

End Module

In this program we first display Even Numbers. In above code “Where” is used as a method in the list to specify a where condition and the desired condition is supplied using Lambda expression. We just name an input parameter on left hand side and following the => operator, we specify the condition to select even numbers. Once the numbers are selected in the variable named “evennumber” of type IEnumerable<int>, we loop through the collection using a foreach loop to get result.

 

Similarly, we find the odd numbers by just 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 output.

 

When we run the program we get the following output.

 

Below are the even numbers

 

2 is an even number

4 is an even number

6 is an even number

8 is an even number

10 is an even number

--------------------------------------------------

Below are the Odd numbers

 

1 is an odd number

3 is an odd number

5 is an odd number

7 is an odd number

9 is an odd number

So we just created one Lambda Expression sample application which uses basic Lambda syntax and stores the result in a variable of type IEnumerable<int> and we can loop through it to display the values.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User