Blog author:
Niladri.Biswas | Posted on: 5/8/2012 | Category:
LINQ Blogs | Views: 824 | Status:
[Member] |
Points: 75
|
Alert Moderator
Consider the below program
class Program
{
static void Main(string[] args)
{
var intCollection = new List{ 1, 2, 3, 4, 5, -1, -2,-3,-4,-5 };
Console.WriteLine("Where Extension Method");
Console.WriteLine("-----------------------");
intCollection
.Where(i => i <= 4)
.ToList()
.ForEach(i => Console.WriteLine(i));
Console.WriteLine(Environment.NewLine + "TakeWhile Extension Method");
Console.WriteLine("---------------------------");
intCollection
.TakeWhile(i => i <= 4)
.ToList()
.ForEach(i => Console.WriteLine(i));
Console.ReadKey(true);
}
}
The output is

We can infer that Where extension method stops when it satifies all the condition where as TakeWhile extension method exhausts when the first
condition trurns out to be false. In other words, Whereextension method examines the whole sequence looking for all the matches but TakeWhile
stops when it encounters the first non-match.
Hope this is clear.
Best Regards,
Niladri Biswas
Found interesting? Add this to: