Posted by: Cirilo Meggiolaro | 02/26/2009

Tip of the day #135 – Skip method – Enumerable class

Today’s tip is about the Skip extension method from the Enumerable class. The idea of this method is to ignore / bypass a specified number of items and return the remaining elements from an IEnumerable<T> collection.

How to…

Imagine we have a list of objects called _customers from Customer type containing 50 items and we want to retrieve the last 20 only. The following code performs this task:

IEnumerable<Customer> last = _customers.Skip(30);

foreach (Customer c in last)
{
    Console.WriteLine(“ID: {0} – Name: {1} – Date of Birth: {2}”, c.ID, c.Name, c.DOB);
}

You may use the following syntax as well:

foreach (Customer c in _customers.Skip(30))
{
    Console.WriteLine(“ID: {0} – Name: {1} – Date of Birth: {2}”, c.ID, c.Name, c.DOB);
}

Enjoy it!

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: