Returns elements from the given collection until the specified condition is true. If the first element itself doesn't satisfy the condition then returns an empty collection.
Signature of TakeWhile():
Public static IEnumerable <TSource> TakeWhile<TSource>(this IEnumerable <TSource> source,Func<TSource,bool>,predicate);
Another Over Load Signature:
Public static IEnumerable <TSource> TakeWhile<TSource>(this IEnumerable <TSource> source,Func<TSource,int,bool>,predicate);
Example I:
int[] numbers = { 1, 5, 8, 4, 9, 3, 6, 7, 2, 0 };
var SkipFirstFiveElement = numbers.TakeWhile(n => n < 9);
Output:
It Will return Of eleament 1,5,8 and 4
Example II :
int[] numbers = { 1, 2, 3, 4, 9, 3, 6, 7, 2, 0 };
var SkipFirstFiveElement = numbers.TakeWhile((n,Index) => n < index);
Output:
It Will return Of element 1,2,3 and 4