C# Linq SkipLast Method


Skip elements from the end and return the remaining elements using the SkipLast() method.

The following is an array.

int[] marks = { 45, 88, 50, 90, 95, 85 };

Now, let us skip two elements from the end using SkipLast() and Lambda Expressions, but this is done after arranging the elements in descending order.

IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 45, 88, 50, 90, 95, 85 };
      IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);
      Console.WriteLine("Skipped the marks of last two students...");
      foreach (int res in selMarks)
      Console.WriteLine(res);
   }
}

Output

Skipped the marks of last two students...
95
90
88
85

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements