C# Queryable Take() Method

CsharpProgrammingServer Side Programming

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