Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements