TakeWhile:
This operator is useful when we want to iterate a long list of items. During iteration we can get list of items by using predicate function.
Let’s take a example where I will generate numbers using IEnumerable range method and get items as per predicate function on specified range.
This loop will run untill condition become false.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { //Get items from 1 to 100 upto values does not var listofitems = Enumerable.Range(1, 100).TakeWhile(i => i % 11 != 0); foreach(int em in listofitems) { Console.WriteLine(em); } Console.ReadLine(); } } }