C# LINQ Partitioning Operators – Take and Skip

Updated on     Kisan Patel

The Partitioning operators in LINQ are used to divide an enumerable sequence into two sections and returning the result set without rearranging the elements with one of the sections that satisfies the condition.

The Take, Skip, TakeWhile and SkipWhile are the Partitioning operators.

The Partitioning operators are useful to navigating easily in the application through your query.

The Take Clause

The Take clause returns the specified number of elements from the input sequence, from the beginning of the sequence.

For Example,

NorthwindEntities _db = new NorthwindEntities();

var customers = _db.Customers.Take(5);

foreach (var customer in customers)
{
        Console.WriteLine(customer.CompanyName + " - " + customer.Country);
}

the output of the above C# program…

linq-take-operator

The Skip Clause

The Skip clause skips the specified number of elements from the input sequence starting from the beginning of the input sequence, and returns the rest of the sequence.

Note: When using Skip clause in query you must use OrderBy method before the method Skip.

For Example,

NorthwindEntities _db = new NorthwindEntities();

var customers = _db.Customers.OrderBy(x => x.Country).Skip(4);

foreach (var customer in customers)
{
        Console.WriteLine(customer.CompanyName + " - " + customer.Country);
}

the output of the above C# program…

linq-skip-operator


C# LINQ

Leave a Reply