Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Lambda Expressions Extension Method 6 – SkipWhile()

Posted by Paul on November 13, 2011

SkipWhile () Extension Method

The SkipWhile() is a tricky method. It takes one condition as argument and iterates over the elements. It will start returning the elements once the condition turned false. It will continue returning the elements until the end of the loop.

It is not similar to Where() condition. Let me give an example. We have 1, 2, 3, 4, 5, 6 in the list and given a condition SkipWhile(i => i != 5) it will return 5 and 6. The method works like below:

First it will take element 1 and as the condition is evaluated to true – it will skip the element.

It will go until element 5, and the condition turns false – then from current element onwards it will return all remaining elements.

Parameters: Condition

Return Value: List of Items


In the following example we are using a list of integers and the Skip() method to select the top n numbers from the list.

private IList<int> _list = new List<int>();

public InitializeList()


// Set data








private void SkipWhileButton_Click(object sender, EventArgs e)


/// SkipWhile method to get a sublist by skipping elements if condition is

/// true and continuing till end without checking condition.. Little tricky

var result = _list.SkipWhile(i => (i != 5));

foreach (int i in result)













Extension Method Snapshot

You can find the snapshot of the SkipWhile() method usage as provided by Visual Studio.


Leave a Reply

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

You are commenting using your 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