How to use Linq TakeWhile() to get int array elements while match a condition in C#

1 Answer

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            var match = arr.TakeWhile(item => item < 5);

            foreach (int n in match)
            {
                Console.WriteLine(n);
            }
        }
    }
}

/*
run:

1
2
3
4

*/

 



answered Jan 22, 2017 by avibootz
edited Jan 22, 2017 by avibootz
...