Author :
Complexity : Beginner

# Partitioning Operator in LINQ

Partitioning Operator in LINQ is used to divide the collection into two parts and return one part as result. There are four Partitioning Operator are available in LINQ as follow:

1. TAKE
2. SKIP
3. TAKEWHILE
4. SKIPWHILE

Now we will discuss each operator one by one.

### TAKE Operator

TAKE operator is used to return some elements from the collection as specified in the Take method. Take keyword is used for this purpose. Below is some example in this category.

### Query to take the first 3 elements from the array.

`````` class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 12, 15 };

//Linq Query
var result_Linq = (from p in numbers select p).Take(3);

//Lambda Expression
var result_Lambda = numbers.Take(3);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

}
}``````

Output

``````Numbers from int array (Using Linq):
3
6
9
Numbers from int array (Using Lambda):
3
6
9``````

### Query to take first 2 employees of IT department with Employee Name and Department.

``````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 new { EmployeeName = p.Emp_Name, DepartmentName = p.Emp_Department }).Take(2);

//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Department == "IT").
Select(p => new { EmployeeName = p.Emp_Name, DepartmentName = p.Emp_Department }).Take(2);

//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
foreach (var item in result_Linq)
{
Console.WriteLine(\$"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
}

//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
foreach (var item in result_Lambda)
{
Console.WriteLine(\$"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
}

}

/// <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" });
employees.Add(new Employee() { Emp_Name = "Ramesh", Emp_Salary = 35000, Emp_Department = "IT" });
return employees;
}
}``````

Output

``````Employee Details (Using Linq):
Employee Name: Rahul , Employee Department: IT
Employee Name: Suresh , Employee Department: IT
Employee Details (Using Lambda):
Employee Name: Rahul , Employee Department: IT
Employee Name: Suresh , Employee Department: IT``````

### SKIP Operator

SKIP operator is used to skip some elements from the collection as specified in the Skip method. Skip keyword is used for this purpose. Below is some example in this category.

### Query to skip the first 3 elements from the array.

`````` class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 12, 15 };

//Linq Query
var result_Linq = (from p in numbers select p).Skip(3);

//Lambda Expression
var result_Lambda = numbers.Skip(3);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

}
}``````

Output

``````Numbers from int array (Using Linq):
12
15
Numbers from int array (Using Lambda):
12
15``````

### Query to skip first 2 employees of IT department with Employee Name and Department.

`````` 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 new { EmployeeName = p.Emp_Name, DepartmentName = p.Emp_Department }).Skip(2);

//Lambda Expression
var result_Lambda = employees.Where(p => p.Emp_Department == "IT").
Select(p => new { EmployeeName = p.Emp_Name, DepartmentName = p.Emp_Department }).Skip(2);

//Display Linq Query Result
Console.WriteLine("Employee Details (Using Linq):");
foreach (var item in result_Linq)
{
Console.WriteLine(\$"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
}

//Display Lambda Expression Result
Console.WriteLine("Employee Details (Using Lambda):");
foreach (var item in result_Lambda)
{
Console.WriteLine(\$"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
}

}

/// <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" });
employees.Add(new Employee() { Emp_Name = "Ramesh", Emp_Salary = 35000, Emp_Department = "IT" });
return employees;
}
}``````

Output

``````Employee Details (Using Linq):
Employee Name: Ramesh , Employee Department: IT
Employee Details (Using Lambda):
Employee Name: Ramesh , Employee Department: IT``````

### TAKEWHILE Operator

TakeWhile operator is used to return the elements from the collection which satisfy the condition specified in the TakeWhile method. TakeWhile keyword is used for this purpose. Below is some example in this category.

### Query to take all elements from the array until a number that is not 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 select p).TakeWhile(p => p < 10);

//Lambda Expression
var result_Lambda = numbers.TakeWhile(p => p < 10);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

}
}``````

Output

``````3
6
9
Numbers from int array (Using Lambda):
3
6
9``````

### Query to take all elements from the array until a number whose value is less than its position in the array.

`````` class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 2, 4 };

//Linq Query
var result_Linq = numbers.TakeWhile((digit, index) => digit >= index);

//Lambda Expression
var result_Lambda = numbers.TakeWhile((digit,index) => digit >= index);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

}
}``````

Output

``````Numbers from int array (Using Linq):
3
6
9
Numbers from int array (Using Lambda):
3
6
9``````

### SKIPWHILE Operator

The SkipWhile operator is used to skip the elements from the collection based on the condition specified in SkipWhile method. SkipWhile keyword is used for this purpose. Below is some example in this category.

### Query to skip all elements from the array until a number that is not 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 select p).SkipWhile(p => p < 10);

//Lambda Expression
var result_Lambda = numbers.SkipWhile(p => p < 10);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

}
}``````

Output

``````Numbers from int array (Using Linq):
12
15
Numbers from int array (Using Lambda):
12
15``````

### Query to skip all elements from the array until a number whose value is less than its position in the array.

`````` class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 2, 4 };

//Linq Query
var result_Linq = numbers.SkipWhile((digit, index) => digit >= index);

//Lambda Expression
var result_Lambda = numbers.SkipWhile((digit,index) => digit >= index);

//Display Linq Query Result
Console.WriteLine("Numbers from int array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}

//Display Lambda Expression Result
Console.WriteLine("Numbers from int array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}

``````Numbers from int array (Using Linq):