SkipWhile Method in Linq

I have been playing around linq and I have found great method call SkipWhile method.  SkipWhile methods skips particular element which matches condition in predicate this can be use full in condition where we need to skip elements on particular condition.

So let’s take some example. I have written following console application code.

using System;
using System.Collections.Generic;
using System.Linq;
namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "Jalpesh", "Jayesh", "Tushar", "Tejas", "Sanjay", "Nijesh" };
            foreach(var name in names.SkipWhile(s=>s.ToLower().StartsWith("j")))
            {
                Console.WriteLine(name); 
            }
        }
    }
}

In the above code I have created a array of string called names and I want to skip names starts with letter “j” with help of ‘SkipWhile’. I am printing each item via for each loop. Following is the output as expected.

image

That’s it.Hope you liked it. Stay tuned for more..Till then happy programming.

Shout it
Published Saturday, April 14, 2012 4:09 AM by Jalpesh P. Vadgama
Filed under: ,

Comments

# re: SkipWhile Method in Linq

Friday, April 13, 2012 6:18 PM by Jonathan Allen

I don't think that is a good example of how it works. Throw some more j-names at the end of the array and you'll see something interesting.