LINQ Partitioning Operators with example | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

The partitioning query operators take an input sequence and partition it, or “return a chunk” in the output sequence. One use of these operators is to break up a larger sequence into “pages,” for example, paging though ten results at a time in a user interface.

1. Take

The Take query operator takes in an input sequence and returns the specified number of elements as an output.
The following code shows how to use the Take query operator to return the first three employee details in  sequence.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQExample
class Program
static void Main(string[] args)
employeedetails[] emp =
new employeedetails{ employeename="Ganesha", department="IT", age=30, emptraining= new List<string>{ "php","asp"}},
new employeedetails{ employeename="Yogesh", department="IT", age=20, emptraining= new List<string>{ "ruby","oracle"}},
new employeedetails{ employeename="Ashish", department="Sales", age=10, emptraining= new List<string>{ "php","oracle"}},
new employeedetails{ employeename="Pooja", department="Teaching", age=15, emptraining= new List<string>{ "java","asp"}},
new employeedetails{ employeename="Arti", department="Teaching", age=40, emptraining= new List<string>{ "java","oracle"}},

IEnumerable<employeedetails> empdetails = emp.Take(2);

foreach(employeedetails em in empdetails)
Console.WriteLine(em.employeename + " " + em.department);

public class employeedetails
public string employeename;
public string department;
public int age;
public List<string> emptraining;

LINQ_Take_Example_outputDownload Source Code

Take can can be chained together with other query operator. In my following example , I am taking those two employees who have age more than 20. Here I am chaining take query operator with Where query operator.

Please make following changes in above code.

//Get those employees who have age more than 20
IEnumerable; empdetails = emp.Where(x=>x.age>20).Take(2);