[LINQ Operators] Skip

The “Skip” operator skips a given number of elements from a sequence and then returns the remainder of the sequence.

 

1. Sample Data Object

To test examples, you need to include the <data classes> in your project.

 

2. “Skip” operator

  • Deferred operator
  • Purpose: Partitioning / Making a subset

 

3. Prototypes

public static IEnumerable<T> Skip<T>(
  this IEnumerable<T> source,
  int count)

The “Skip()” operator bypasses a specified number of elements from the beginning of a sequence and then returns the remaining elements. It is useful for paging.

 

4. Example 1 (Standard Operator)

var emps = Employee.GetEmployees();

var newEmps
  = emps.OrderByDescending(e => e.Name).Skip(3);

foreach (Employee e in newEmps)
{
  Console.WriteLine($"{e.Id}: {e.Name}");
}

 

5. Example 2 (Query Expression)

There’s no direct  translation to a query expression. But you can use the “Skip()” operator upon the whole query expression.

var emps = Employee.GetEmployees();

var newEmps
= (from e in emps
   orderby e.Name descending
   select e).Skip(3);

foreach (Employee e in newEmps)
{
  Console.WriteLine($"{e.Id}: {e.Name}");
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s