C# Queryable TakeLast() Method


Get specified number of elements from the end using the TakeLast() method.

The following is our array.

int[] pages = { 492, 290, 129, 602, 152 };

Now, use OrderBy to order the elements in ascending order. Then use the TakeLast() method to get specified number of elements from the end.

marks.AsQueryable().OrderByDescending(s => s).Take(5);

Let us see the complete example.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      // pages of books
      int[] pages = { 492, 290, 129, 602, 152 };
      // get pages of last two books
      IEnumerable<int> last = pages.AsQueryable().OrderBy(s => s).TakeLast(2);
      foreach (int res in last) {
         Console.WriteLine(res);
      }
   }
}

Output

492
602

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements