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

kick it on DotNetKicks.com

2 comments:

  1. Sorry if I'm being dense; couldn't you also use a Where clause for this?

    ReplyDelete
  2. blaster151 - There is major difference between skipwhile and where. Where filters the things. While skipwhile skip the elements until condition is true . I will post a blog on that

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.