LINQ TakeWhile Query Operator

By Peter Bromberg

Returns elements from a sequence as long as a specified condition is true.

string[] fruits = { "apple", "banana", "mango", "orange",
                                    "passionfruit", "grape" };

           IEnumerable<string> query =
                 fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);

             foreach (string fruit in query)
            {
               Console.WriteLine(fruit);
            }

            /*
             This code produces the following output:

             apple
             banana
             mango
             */

LINQ TakeWhile Query Operator  (868 Views)