C# Queryable Take() Method


Get specified number of elements from the beginning using the Take() method.

The following is our array.

int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 };

Now, use OrderByDescending to order the elements in Descending order. Then use the Take() method to get the elements.

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() {
      int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 };
      // top 5 student marks
      IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).Take(5);
      foreach (int res in selMarks) {
         Console.WriteLine(res);
      }
   }
}

Output

95
90
85
72
67

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements