;

Lambda Expression


In LINQ, Lambda expressions play a very crucial role when using method syntax. Lambda expressions provide a concise and expressive way to represent anonymous functions or delegates, making it easier to write inline functions for filtering, projecting, and performing other operations on data.

Basics of Lambda Expressions:

A lambda expression is an anonymous function that can have parameters and a body. You can use the lambda declaration operator => to separate the lambda's parameter list from its body. It has the following general syntax as given below:

Syntax 

Expression lambda that has an expression as its body:

(input-parameters) => expression

Statement lambda that has a statement block as its body:

(input-parameters) => { <sequence-of-statements> }

Where,

  • input-parameters: These are the input parameters to the function. If there's only one parameter, parentheses are optional. If there are no parameters, you still need empty parentheses.
  • =>: The lambda operator separates the parameter list from the expression or statement block.
  • expression or sequence-of-statements: This is the body of the function. If it's a single expression, it becomes the return value. If it's a block, you need to explicitly use the return keyword.

Examples of Lambda Expression

Example 1: Lambda Expression with Multiple Parameters

In this example, you can wrap the parameters in the parenthesis if you want to pass more than one parameter. Here's an example of a Lambda expression with multiple parameters  in LINQ:

Example 1: Lambda Expression with Multiple Parameters
using System;
					
public class Program
{
	delegate bool IsSalaryGreaterThan(Employee emp, int greaterSalary);
	
	public static void Main()
	{
		IsSalaryGreaterThan isSalaryGreaterThan = (s, greaterSalary) => s.Salary > greaterSalary;
		
		Employee emp = new Employee() { Id = 1, Name = "John Wick", Salary = 25000 };
		
		Console.WriteLine($"Is Salary Greater than 25000? {isSalaryGreaterThan(emp, 26000)}");
		
		Console.WriteLine($"Is Salary Greater than 25000? {isSalaryGreaterThan(emp, 20000)}");
	}
}

public class Employee{

	public int Id { get; set; }
	public string Name { get; set; }
	public int Salary { get; set; }
}
Output
Is Salary Greater than 25000? False
Is Salary Greater than 25000? True

If you are confused with the parameter type, then you can also give the type of each parameter. Here's an example of giving the type of each parameter: 

(Employee s, int greaterSalary) => s.Salary > greaterSalary;

Example 2: Lambda Expression without Parameter

You can also use the lambda expression without passing any parameters. Here's an example of a Lambda expression without a parameter in LINQ:

Example 2: Lambda Expression without Parameter
using System;
					
public class Program
{
	delegate void Print();
	
	public static void Main()
	{
		Print print = () => Console.WriteLine("Here, we are using Lambda expression without passing any parameter");
		print();		
	}
}
Output
Here, we are using Lambda expression without passing any parameter

Example 3: Multiple Statements in Lambda Expression Body

You can also use more than one statement with the lambda expression in the body. You just need to wrap the expression in the curly braces. Here's an example of Multiple Statements in Lambda Expression Body in LINQ:

Example 3: Multiple Statements in Lambda Expression Body
using System;
					
public class Program
{
	delegate bool IsSalaryGreaterThan(Employee emp, int greaterSalary);
	
	public static void Main()
	{
		IsSalaryGreaterThan isSalaryGreaterThan = (s, greaterSalary) => {
			
			Console.WriteLine("Here, we are using Lambda expression with multiple statements in the body");
			
			return s.Salary > greaterSalary;
		};
		
		Employee emp = new Employee() { Id = 1, Name = "John Wick", Salary = 25000 };
		
		Console.WriteLine($"Is Salary Greater than 25000? {isSalaryGreaterThan(emp, 26000)}");
	}
}

public class Employee{

	public int Id { get; set; }
	public string Name { get; set; }
	public int Salary { get; set; }
}
Output
Here, we are using Lambda expression with multiple statements in the body
Is Salary Greater than 25000? False

Example 4: Declare Local Variable in Lambda Expression Body

You can also declare a variable in the lambda expression body and use it anywhere in the expression body. Here's an example of declaring a local variable in the lambda expression body in LINQ:

Example 4: Declare Local Variable in Lambda Expression Body
using System;
					
public class Program
{
	delegate bool IsSalaryGreaterThan(Employee emp);
	
	public static void Main()
	{
		IsSalaryGreaterThan isSalaryGreaterThan = (s) => {
			
			//Declaring a Local Variable and use it anywhere in the body
			int greaterSalary = 20000;
			
			Console.WriteLine("Here, we are declaring a variable and use in the body");
			
			return s.Salary > greaterSalary;
		};
		
		Employee emp = new Employee() { Id = 1, Name = "John Wick", Salary = 25000 };
		
		Console.WriteLine($"Is Salary Greater than 25000? {isSalaryGreaterThan(emp)}");
	}
}

public class Employee{

	public int Id { get; set; }
	public string Name { get; set; }
	public int Salary { get; set; }
}
Output
Here, we are declaring a variable and use in the body
Is Salary Greater than 25000? True

Example 5: Assign Lambda Expression to Delegate

In Linq, you can assign a lambda expression to a delegate like Func, Action, and Predicate. This allows you to create delegate instances inline without having to explicitly define a separate method. Here's an example of assigning lambda expression to delegate in LINQ:

Example 5: Assign Lambda Expression to Delegate
using System;
					
public class Program
{
	// Define a delegate type
	delegate bool IsSalaryGreaterThan(Employee emp, int greaterSalary);
	
	public static void Main()
	{
		// Assign a lambda expression to the delegate
		IsSalaryGreaterThan isSalaryGreaterThan = (s, greaterSalary) => {
			
			Console.WriteLine("Here, we are Assigning a lambda expression to the delegate");
			
			return s.Salary > greaterSalary;
		};
		
		Employee emp = new Employee() { Id = 1, Name = "John Wick", Salary = 25000 };
		
		//Call the Delegate
		Console.WriteLine($"Is Salary Greater than 25000? {isSalaryGreaterThan(emp, 26000)}");
	}
}

public class Employee{

	public int Id { get; set; }
	public string Name { get; set; }
	public int Salary { get; set; }
}
Output
Here, we are Assigning a lambda expression to the delegate
Is Salary Greater than 25000? False

Learn Standard Query Operators in the next tutorial.