LINQ-TakeWhile to return elements starting from the beginning of the array until a number is hit that is not less than 6.

This Function uses TakeWhile to return elements starting from the beginning of the array until a number is hit that is not less than 6.

        public void TakeWhileExample()
        {

            int[] numbers = {1,3,4,7,8,4,3,2,1};



            var NumbersLessThan6 = numbers.TakeWhile(n => n < 6);



            Console.WriteLine("Numbers less than 6:");

            foreach (var n in NumbersLessThan6)
            {

                Console.WriteLine(n);

            }

         }

Ebook Download
View all
Learn
View all