Linq的TakeWhile的用法

http://www.codewars.com/kata/56676e8fabd2d1ff3000000c/train/csharp

Can you find the needle in the haystack?

Write a function findNeedle() that takes an array full of junk but containing one 'needle'

After your function finds the needle it should return a message (as a string) that says:

'found the needle at position ' plus the index it found the needle

So

findNeedle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'])

should return

'found the needle at position 5'
 public static string FindNeedle(object[] haystack)
        {
            const string needle = "needle";
            var strings = haystack.Select(x => x.ToString());
            int position = strings.TakeWhile(str => !str.Equals(needle)).Count();
            return $"found the needle at position {position}";
        }
原文地址:https://www.cnblogs.com/chucklu/p/5084696.html