Element Operator in LINQ
Element Operator in LINQ is used to return the first or last or single element from the collection. There are some Element Operator are available in LINQ as follow:
- FIRST
- FIRSTORDEFAULT
- LAST
- LASTORDEFAULT
- ELEMENTAT
- ELEMENTATORDEFAULT
- SINGLE
- SINGLEORDEFAULT
- DEFAULTIFEMPTY
Now we will discuss each operator one by one.
FIRST Operator
FIRST operator is used to return the first element from the collection or the first element from the collection which satisfies the given condition. It will throw an exception if there is no element within the collection or there is no element within the collection that satisfies the given condition. First keyword is used for this purpose. Below are some examples in this category.
Query to find the first number which is greater than 10.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p>10 select p).First();
//Lambda Expression
var result_Lambda = numbers.First(p => p > 10);
//Display Linq Query Result
Console.WriteLine($"First Numbers greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"First Numbers greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
First Number greater than 10 (Using Linq): 12
First Number greater than 10 (Using Lambda): 12
Query to find the first matching employee in the list.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "IT" select p).First();
//Lambda Expression
var result_Lambda = employees.First(p => p.Emp_Department == "IT");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Details (Using Lambda):
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
FIRSTORDEFAULT Operator
FIRSTORDEFAULT operator is used to return the first element from the collection or the first element from the collection which satisfies the given condition. It will return default value null if there is no element within the collection or there is no element within the collection that satisfies the given condition. FirstOrDefault keyword is used for this purpose. Below are some examples in this category.
Query to find the first matching element in the array and return the default value if there is no matching element.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 20 select p).FirstOrDefault();
//Lambda Expression
var result_Lambda = numbers.FirstOrDefault(p => p > 20);
//Display Linq Query Result
Console.WriteLine($"First number greater than 20 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"First number greater than 20 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
First number greater than 10 (Using Linq): 0
First number greater than 10 (Using Lambda): 0
Query to find the first matching employee in the list and return the default value if there is no matching employee.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "Admin" select p).FirstOrDefault();
//Lambda Expression
var result_Lambda = employees.FirstOrDefault(p => p.Emp_Department == "Admin");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
No Employee Found.
Employee Details (Using Lambda):
No Employee Found.
LAST Operator
LAST operator is used to return the last element from the collection or the last element from the collection which satisfies the given condition. It will throw an exception if there is no element within the collection or there is no element within the collection that satisfies the given condition. Last keyword is used for this purpose. Below are some examples in this category.
Query to find the last number which is greater than 10.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 10 select p).Last();
//Lambda Expression
var result_Lambda = numbers.Last(p => p > 10);
//Display Linq Query Result
Console.WriteLine($"Last Number greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Last Number greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Last Numbers greater than 10 (Using Linq): 17
Last Numbers greater than 10 (Using Lambda): 17
Query to find last matching employee in the list.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "IT" select p).Last();
//Lambda Expression
var result_Lambda = employees.Last(p => p.Emp_Department == "IT");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Details (Using Lambda):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
LASTORDEFAULT Operator
LASTORDEFAULT operator is used to return the last element from the collection or the last element from the collection which satisfies the given condition. It will return default value null if there is no element within the collection or there is no element within the collection that satisfies the given condition. LastOrDefault keyword is used for this purpose. Below are some examples in this category.
Query to find the last matching element in the array and return the default value if there is no matching element.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 20 select p).LastOrDefault();
//Lambda Expression
var result_Lambda = numbers.LastOrDefault(p => p > 20);
//Display Linq Query Result
Console.WriteLine($"Last Number greater than 20 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Last Number greater than 20 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Last Number greater than 20 (Using Linq): 0
Last Number greater than 20 (Using Lambda): 0
Query to find the last matching employee in the list and return the default value if there is no matching employee.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "Admin" select p).LastOrDefault();
//Lambda Expression
var result_Lambda = employees.LastOrDefault(p => p.Emp_Department == "Admin");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
No Employee Found.
Employee Details (Using Lambda):
No Employee Found.
ELEMENTAT Operator
ELEMENTAT operator is used to return the element from the collection at a specified index position. It will throw an exception if there is no element within the collection at a specified index position. ElementAt keyword is used for this purpose. Below are some examples in this category.
Query to find a second number which is greater than 10.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p>10 select p).ElementAt(1);
//Lambda Expression
var result_Lambda = numbers.Where(p => p > 10).ElementAt(1);
//Display Linq Query Result
Console.WriteLine($"Second Number greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Second Number greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Second Number greater than 10 (Using Linq): 15
Second Number greater than 10 (Using Lambda): 15
Query to find a second matching employee in the list.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "IT" select p).ElementAt(1);
//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Department == "IT").ElementAt(1);
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Details (Using Lambda):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
ELEMENTATORDEFAULT Operator
ELEMENTATORDEFAULT operator is used to return the element from the collection at a specified index position. It will return default value null if there is no element within the collection at a specified index position. ElementAtOrDefault keyword is used for this purpose. Below are some examples in this category.
Query to find the second matching element in the array and return the default value if there is no matching element.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 20 select p).ElementAtOrDefault(1);
//Lambda Expression
var result_Lambda = numbers.Where(p => p > 20).ElementAtOrDefault(1);
//Display Linq Query Result
Console.WriteLine($"Second Number greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Second Number greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Second Number greater than 10 (Using Linq): 0
Second Number greater than 10 (Using Lambda): 0
Query to find a second matching employee in the list and return the default value if there is no matching employee.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "HR" select p).ElementAtOrDefault(1);
//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Department == "HR").ElementAtOrDefault(1);
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
No Employee Found.
Employee Details (Using Lambda):
No Employee Found.
SINGLE Operator
SINGLE operator is used to return the element from the collection which contains only a single element or element from the collection which satisfies the given condition and returns only a single element. It will throw an exception if there is no element within the collection or there is no element within the collection that satisfies the given condition or there is more than one element within the collection or there is more than one element within the collection that satisfies the given condition. Single keyword is used for this purpose. Below are some examples in this category.
Query to find the single matching element in the array.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 15 select p).Single();
//Lambda Expression
var result_Lambda = numbers.Single(p => p > 15);
//Display Linq Query Result
Console.WriteLine($"Single Numbers greater than 15 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Single Numbers greater than 15 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Single Numbers greater than 15 (Using Linq): 17
Single Numbers greater than 15 (Using Lambda): 17
Query to find single matching employee in the list.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "HR" select p).Single();
//Lambda Expression
var result_Lambda = employees.Single(p => p.Emp_Department == "HR");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Details (Using Lambda):
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
SINGLEORDEFAULT Operator
SINGLEORDEFAULT operator is used to return the element from the collection which contains only a single element or element from the collection which satisfies the given condition and returns only a single element. It will return default value null if there is no element within the collection or there is no element within the collection that satisfies the given condition. It will throw an exception if there is more than one element within the collection or there is more than one element within the collection that satisfies the given condition. Single keyword is used for this purpose. Below are some examples in this category.
Query to find the single matching element in the array and return the default value if there is no matching element.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 20 select p).SingleOrDefault();
//Lambda Expression
var result_Lambda = numbers.SingleOrDefault(p => p > 20);
//Display Linq Query Result
Console.WriteLine($"Single Number greater than 20 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Single Number greater than 20 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Single Number greater than 20 (Using Linq): 0
Single Number greater than 20 (Using Lambda): 0
Query to find a single matching employee in the list and return the default value if there is no matching employee.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "Admin" select p).SingleOrDefault();
//Lambda Expression
var result_Lambda = employees.SingleOrDefault(p => p.Emp_Department == "Admin");
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
Console.WriteLine($"Employee Name: {result_Linq.Emp_Name}, Employee Department: {result_Linq.Emp_Department}, Employee Salary: {result_Linq.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
Console.WriteLine($"Employee Name: {result_Lambda.Emp_Name}, Employee Department: {result_Lambda.Emp_Department}, Employee Salary: {result_Lambda.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
No Employee Found.
Employee Details (Using Lambda):
No Employee Found.
DEFAULTIFEMPTY Operator
DEFAULTIFEMPTY operator is used to return the default value null if there is no element within the collection. DefaultIfEmpty keyword is used for this purpose. Below are some examples in this category.
Query to return the default value if there is no matching element in the array.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 12, 15, 17 };
//Linq Query
var result_Linq = (from p in numbers where p > 20 select p).DefaultIfEmpty(-1);
//Lambda Expression
var result_Lambda = numbers.Where(p => p > 20).DefaultIfEmpty(-1);
//Display Linq Query Result
foreach (var item in result_Linq)
{
Console.WriteLine($"Number greater than 20 (Using Linq): {item}");
}
//Display Lambda Expression Result
foreach (var item in result_Lambda)
{
Console.WriteLine($"Number greater than 20 (Using Lambda): {item}");
}
Console.Read();
}
}
Output
Number greater than 20 (Using Linq): -1
Number greater than 20 (Using Lambda): -1
Query to return the default value if there is no matching employee in the list.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Department == "Admin" select p).DefaultIfEmpty();
//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Department == "Admin").DefaultIfEmpty();
//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
if (result_Linq != null)
{
foreach (Employee emp in result_Linq)
{
if (emp != null)
{
Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
}
}
//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
if (result_Lambda != null)
{
foreach (Employee emp in result_Lambda)
{
if (emp != null)
{
Console.WriteLine($"Employee Name: {emp.Emp_Name}, Employee Department: {emp.Emp_Department}, Employee Salary: {emp.Emp_Salary}");
}
else
{
Console.WriteLine("No Employee Found.");
}
}
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Employee Details (Using Linq):
No Employee Found.
Employee Details (Using Lambda):
No Employee Found.