C# LINQ 基础 互动版

SkipWhile

TakeWhile操作符用于跳过满足一定条件指定数量的元素。用法如下代码:

static void Main()
{
  int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
  // SkipWhile的用法
  IEnumerable&ltint&gt lowerGrades =
  grades.OrderByDescending(grade => grade).SkipWhile(grade => grade >= 80);
  Console.WriteLine("All grades below 80:");
  foreach (int grade in lowerGrades)
  {
      Console.WriteLine(grade);
  }
}

运行结果为:

All grades below 80:
70
59
56