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.
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:
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,
=>
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:
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; }
}
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;
You can also use the lambda expression without passing any parameters. Here's an example of a Lambda expression without a parameter in LINQ:
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();
}
}
Here, we are using Lambda expression without passing any parameter
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:
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; }
}
Here, we are using Lambda expression with multiple statements in the body
Is Salary Greater than 25000? False
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:
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; }
}
Here, we are declaring a variable and use in the body
Is Salary Greater than 25000? True
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:
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; }
}
Here, we are Assigning a lambda expression to the delegate
Is Salary Greater than 25000? False
Learn Standard Query Operators in the next tutorial.