C# Linq SkipLast Method

CsharpProgrammingServer Side Programming

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
raja
Published on 14-Sep-2018 15:35:11
Advertisements