Differences between Take() and TakeWhile() extension methods in LINQ       S.No     Take()                              Ta...
Even though the collection has the items which are less than 9 such as 8,6,7,2,and 0 but itreturned only 5,4,1 and 3. The ...
Upcoming SlideShare
Loading in …5
×

Differences between take() and takewhile() extension methods in linq

1,841 views

Published on

Differences between Take() and TakeWhile() extension methods in LINQ

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,841
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Differences between take() and takewhile() extension methods in linq

  1. 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.gifExample 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 first5 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 conditionfails. i.e. if the control reaches the item 9 then the condition fails here so it will return theitems from 5 to 3 [5, 4, 1, 3].Quick Note:
  2. 2. Even though the collection has the items which are less than 9 such as 8,6,7,2,and 0 but itreturned only 5,4,1 and 3. The point here is the TakeWhile wont consider the items whichare 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 4characters i.e. it will return only the "one" and "two" because the length of the "three" is 5so 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 arrayuntil a string length is greater than its position in the collection.So this example will return "one" and "two". Because the "three" has the length of 5 and itsposition [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 myblog @ http://onlydifferencefaqs.blogspot.in/

×