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

No comments:

Post a Comment

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