Skip
TakeWhile
SkipWhile.
Take operators takes given number of elements from a sequence (collection). I will explain with an example,
SKIP
TakeWhile
TakeWhile take takes sequence in a collection until the mentioned condition is true. I will explain with an example.
Condider an array of numbers as below,
int[] randomNumbers = { 10, 6, 4, 8, 5, 3, 2, 1 };
consider a query like this,
randomNumbers.TakeWhile(a => a > 3)
output would be “10,6,4,8,5”. So TakeWhile takes numbers from a collection until the condition becomes false.
Whole line to print result using lambda is,
randomNumbers.TakeWhile(a => a > 3).ToList().ForEach(a => Console.WriteLine(a));
SkipWhile.
SkipWhile is the reverse of TakeWhile. SkipWhile skips sequence from a collection until the specified condition is true. I will explain with an example,
randomNumbers.SkipWhile(a => a > 3)
for the query the output would be “3,2,1” which means SkipWhile skip elements until the true condition met in a collection. Whole line to print result using lambda is ,
randomNumbers.SkipWhile(a => a > 3).ToList().ForEach(a => Console.WriteLine(a));
No comments:
Post a Comment