LINQ where vs takewhile

I want to get the difference between takewhile and LINQ methods. I got the following data from MSDN. But that didn't make sense to me.

Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) 

Filters a sequence of values ​​based on a predicate.

 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) 

Returns elements from a sequence if the specified condition is true.

All opinions are welcome.

+68
linq
Feb 17 '11 at 16:33
source share
6 answers

TakeWhile stops when the condition is false, where it continues and finds all elements matching the condition

 var intList = new int[] { 1, 2, 3, 4, 5, -1, -2 }; Console.WriteLine("Where"); foreach (var i in intList.Where(x => x <= 3)) Console.WriteLine(i); Console.WriteLine("TakeWhile"); foreach (var i in intList.TakeWhile(x => x <= 3)) Console.WriteLine(i); 

gives

 Where 1 2 3 -1 -2 TakeWhile 1 2 3 
+117
Feb 17 '11 at 16:37
source share

Where can check the entire sequence looking for matches.

 Enumerable.Range(1, 10).Where(x => x % 2 == 1) // 1, 3, 5, 7, 9 

TakeWhile stops looking when it encounters the first mismatch.

 Enumerable.Range(1, 10).TakeWhile(x => x % 2 == 1) // 1 
+24
Feb 17 '11 at 16:35
source share

MSDN says

Enumerable.TakeWhile Method

Returns elements from a sequence as if the specified condition is true, and then skips the remaining elements.

Enumerable.Where

Filters a sequence of values ​​based on a predicate.

The difference is that Enumerable.TakeWhile skips the remaining elements from the first mismatch, regardless of whether they match the condition

+7
Feb 17 '11 at 16:44
source share

Let's say you have an array containing [1, 3, 5, 7, 9, 0, 2, 4, 6, 8] . Now:

var whereTest = array.Where(i => i <= 5); will return [1, 3, 5, 0, 2, 4] .

var whileTest = array.TakeWhile(i => i <= 5); will return [1, 3, 5] .

+6
Feb 17 '11 at 16:39
source share

While the existing answers are correct, none of them indicate why you want to use TakeWhile if the results are the same: Performance. Suppose you have an ordered list with 2 billion items in it, and you want those that (probably 10 or 15 items) to be less than a given value. Where clause will consider all 2 billion elements, while TakeWhile will stop as soon as it finds a value equal to or greater than your given value

+5
Oct 29 '15 at 15:21
source share

The order of the sequence passed is absolutely critical with TakeWhile , which will end as soon as the predicate returns false , while Where will continue to evaluate the sequence outside the first false .

A common use for TakeWhile is a lazy evaluation of large, expensive, or even infinite enumerated objects, where you may have additional knowledge about ordering a sequence.

eg. Given the sequence:

 IEnumerable<BigInteger> InfiniteSequence() { BigInteger sequence = 0; while (true) { yield return sequence++; } } 

A .Where will lead to an infinite loop trying to evaluate part of the enumerated:

 var result = InfiniteSequence() .Where(n => n < 100) .Count(); 

While a .TakeWhile and armed with the knowledge that enumerated objects are ascending, will allow to evaluate the partial sequence:

 var result = InfiniteSequence() .TakeWhile(n => n < 100) .Count(); 
+4
Jun 21 '14 at 11:04
source share



All Articles