Linq的TakeWhile的用法
Posted chucklu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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}"; }
以上是关于Linq的TakeWhile的用法的主要内容,如果未能解决你的问题,请参考以下文章