TakeWhile使用查询语法

Ano*_*nym 1 c# linq

public static IEnumerable<long> FibonacciNumbers() {
 long current = 0;
 long next = 1;

 while (true) {
  long previous = current;
  current = next ;
  next = previous + next;
  yield return current;
 }
}
Run Code Online (Sandbox Code Playgroud)

我可以得到第一个斐波那契数字少于100

var series = FibonacciNumbers().TakeWhile(num => num < 100);
Run Code Online (Sandbox Code Playgroud)

只是好奇,我怎么用查询语法呢?

Jon*_*eet 5

你不会 - 在C#查询表达式中没有任何内容对应TakeWhile(或Take,Skip,SkipWhile等).C#查询表达式相对有限,但涵盖了biggies:

  • 选择(通过selectlet)
  • 哪里(通过where)
  • SelectMany(通过二级from条款)
  • OrderBy/ThenBy(和降序)(通过orderby子句)
  • 加入(通过join条款)
  • GroupBy(通过groupby条款)
  • GroupJoin(通过join ... into条款)

VB 9的查询支持有点广泛,但我个人喜欢C#的方法 - 它保持语言相对简单,但你仍然可以通过点表示法做你想做的一切.