Take While
The TakeWhile
method takes a boolean predicate as a parameter. Similar to how the SkipWhile
method works, the only difference is that rather than skipping, it takes. So, based on the boolean condition it will take each item so long as the condition holds true. On the first occurrence, it is false, it will stop and return those items.
Basic example
Here, we have a list and the result is just 87 and 56. It didn't return 100 as well. Why? Because at 33, it was the first occurrence where the boolean predicate didn't hold true. It was false. 33 is not greater than 50, so it stopped there.
Live example: https://dotnetfiddle.net/zbiCoC
var scores = new[] { 87, 56, 33, 20, 11, 100 };
var newScores = scores.TakeWhile(i => i > 50); // take each element until the first occruance of the condition is not met (false)
//output: 87,56