Designing IA for AI - Information Architecture Conference 2024
Differences between take() and takewhile() extension methods in linq
1. Differences between Take() and TakeWhile() extension methods in LINQ
S.No Take() TakeWhile()
1 Meaning: Meaning:
The Take (int...) method of The TakeWhile (Func...) will return
System.Linq.Queryable class elements starting from the
returns the portion of a source beginning of an IEnumerable
i.e. an IEnumerable collection collection until it satisfies the
of a source with the first n condition specified. If the condition
number of items from the is false then it will return the
source. collection immediately.
2 Signature of Take (): Signature of TakeWhile ():
http://www.c- http://www.c-
sharpcorner.com/UploadFile/d sharpcorner.com/UploadFile/dbd95
bd951/how-to-use- 1/how-to-use-taketakewhile-and-
taketakewhile-and- skipskipwhile-in-
skipskipwhile-in- linq/Images/Linq2.gif
linq/Images/Linq1.gif Another signature for overriding
the method:
http://www.c-
sharpcorner.com/UploadFile/dbd95
1/how-to-use-taketakewhile-and-
skipskipwhile-in-
linq/Images/Linq3.gif
Example for Take() extension method:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.Take(5);
In the above example the Take method returns an IEnumerable collection with only the first
5 items as we have specified in the Take argument.
I.e. it will return 5, 4, 1, 3 and 9 only.
Examples for TakeWhile() extension method:
Example I:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 9);
In the above example the TakeWhile will return the collection of items when the condition
fails. i.e. if the control reaches the item 9 then the condition fails here so it will return the
items from 5 to 3 [5, 4, 1, 3].
Quick Note:
2. Even though the collection has the items which are less than 9 such as 8,6,7,2,and 0 but it
returned only 5,4,1 and 3. The point here is the TakeWhile won't consider the items which
are the item which makes the condition fail.
Example II:
String colection = { "one", "two", "three", "four", "five" };
var strings = collection.TakeWhile(n => n.Length < 4);
In this example, the TakeWhile will return the string collection having a length of 4
characters i.e. it will return only the "one" and "two" because the length of the "three" is 5
so it fails there.
Example III:
String colection = { "one", "two", "three", "four", "five" };
var strings = collection.TakeWhile((s, index)=> s.Length > index );
In this example the TakeWhile will return the items starting from the beginning of the array
until a string length is greater than it's position in the collection.
So this example will return "one" and "two". Because the "three" has the length of 5 and its
position [index] is 2. Since 2 is less than 5 the condition fails at "three".
Reference:
http://www.c-sharpcorner.com/uploadfile/dbd951/how-to-use-taketakewhile-and-
skipskipwhile-in-linq/
And, further updates on difference between questions and answers, please visit my
blog @ http://onlydifferencefaqs.blogspot.in/