Author : Rahul
Last Modified : 13-Jul-2021
Complexity : Beginner
Restriction Operator in LINQ
Restriction Operator is used to filter data based on some condition. Just like SQL WHERE keyword is used to filter the data. Below are some examples in this category.
Query to find all elements of an array that is less than 10
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 12, 15 };
//Linq Query
var result_Linq = from p in numbers where p < 10 select p;
//Lambda Expression
var result_Lambda = numbers.Where(p => p < 10);
//Display Linq Query Result
Console.WriteLine("Numbers less than 10 (Using Linq):");
foreach(int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Numbers less than 10 (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Numbers less than 10 (Using Linq):
3
6
9
Numbers less than 10 (Using Lambda):
3
6
9
Query to find all employees having a salary is greater than 50000
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_Salary > 50000 select p;
//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Salary > 50000);
//Display Linq Query Result
Console.WriteLine("Employees having salary greater than 50000 (Using Linq):");
foreach(Employee item in result_Linq)
{
Console.WriteLine("Employee Name: " + item.Emp_Name);
}
//Display Lambda Expression Result
Console.WriteLine("Employees having salary greater than 50000 (Using Lambda):");
foreach (Employee item in result_Lambda)
{
Console.WriteLine("Employee Name: " + item.Emp_Name);
}
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 = "IT" });
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 = "IT" });
return employees;
}
}
Output
Employees having salary greater than 50000 (Using Linq):
Employee Name: Rahul
Employee Name: Nitish
Employees having salary greater than 50000 (Using Lambda):
Employee Name: Rahul
Employee Name: Nitish
Query to find all employees in IT department having a salary is greater than 50000
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_Salary > 50000 && p.Emp_Department == "IT" select p;
//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Salary > 50000 && p.Emp_Department == "IT");
//Display Linq Query Result
Console.WriteLine("Employees in IT Department having salary greater than 50000 (Using Linq):");
foreach(Employee item in result_Linq)
{
Console.WriteLine("Employee Name: " + item.Emp_Name);
}
//Display Lambda Expression Result
Console.WriteLine("Employees in IT Department having salary greater than 50000 (Using Lambda):");
foreach (Employee item in result_Lambda)
{
Console.WriteLine("Employee Name: " + item.Emp_Name);
}
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
Employees in IT Department having salary greater than 50000 (Using Linq):
Employee Name: Rahul
Employees in IT Department having salary greater than 50000 (Using Lambda):
Employee Name: Rahul
Query to find numbers having length is less than their value
class Program
{
static void Main(string[] args)
{
string[] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight" };
//Linq Query
var result_Linq = numbers.Where((number, index) => number.Length < index);
//Lambda Expression
var result_Lambda = numbers.Where((number, index) => number.Length < index);
//Display Linq Query Result
Console.WriteLine("Numbers having length is less than its value (Using Linq):");
foreach(var item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Numbers having length is less than its value (Using Lambda):");
foreach (var item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Numbers having length is less than its value (Using Linq):
five
six
seven
eight
Numbers having length is less than its value (Using Lambda):
five
six
seven
eight