Having
public static IEnumerable<long> FibonacciNumbers() {
long current = 0;
long next = 1;
while (true) {
long previous = current;
c开发者_JS百科urrent = next ;
next = previous + next;
yield return current;
}
}
I can get the first fibonacci numbers less that 100 with
var series = FibonacciNumbers().TakeWhile(num => num < 100);
Just being curious, how would I do that using query syntax ?
You wouldn't - there's nothing in C# query expressions that corresponds to TakeWhile
(or Take, Skip, SkipWhile etc). C# query expressions are relatively limited, but cover the biggies:
- Select (via
select
andlet
) - Where (via
where
) - SelectMany (via secondary
from
clauses) - OrderBy/ThenBy (and descending) (via
orderby
clauses) - Join (via
join
clauses) - GroupBy (via
groupby
clauses) - GroupJoin (via
join ... into
clauses)
VB 9's query support is a bit more extensive, but personally I like C#'s approach - it keeps the language relatively simple, but you can still do everything you want via dot notation.
There's no built in syntax for this in LINQ. Besides, writing it in LINQ would be more verbose and wouldn't really aid clarity, so there's no real need in this case.
Also, you should use Take
rather than TakeWhile
here.
精彩评论