Saturday, April 14, 2012

TakeWhile operator in linq

In this post I am going to explain TakeWhile Operator in details. TakeWhile is one of partitioning operator available in Linq. It will take sequence until condition becomes false.

Here is the example for that.

using System;
using System.Collections.Generic;
using System.Linq;
namespace Linq
{
class Program
{
  static void Main(string[] args)
  {
      int[] numbers ={10,9,8,7,6,5,4,3,2,1};

      foreach(var number in numbers.TakeWhile(n=>n>5))
      {
          Console.WriteLine(number);
      }
  }
}
}


In the above code you can see that I taken an array of number from 10 to 1 and I am printing number with take while operator and I have putted condition like number should be greater then 5. So it will take sequence of all the number until conditions becomes false.Following is the output as expected.



Take while operator in C#

That’s it. Hope you like it. Stay tuned for more. Till than happy programming.

Shout it

kick it on DotNetKicks.com
If you enjoyed this post, make sure you subscribe to the dotnetjalps RSS feed to receive new posts in a reader or via email!. You can subcribe via putting email address in below box also
Subscribe via Email

0 comments:

Post a Comment

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