C# Program to skip elements from a sequence as long as the specified condition is true


Use the SkipWhile() method to skip elements from a sequence as long as the specified condition is true.

The following is the array −

int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };

Here is the condition.

s => s >= 50

As long as the above condition is true, the elements above 50 are skipped as shown below −

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };
      // skips elements above 50
      IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >=          50);
      // displays rest of the elements
      Console.WriteLine("Skipped marks > 60...");
      foreach (int res in selMarks) {
         Console.WriteLine(res);
      }
   }
}

Output

Skipped marks > 60...
48
42
35

Updated on: 23-Jun-2020

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements