
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# Program to Return Specified Number of Elements from the Beginning of a Sequence
Set an array and arrange it in descending order using OrderByDescending.
int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};
Now, use the Take() method to return specified number of elements from the beginning.
Enumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2);
Let us see the complete code.
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345}; // Volume of top two products IEnumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2); foreach (int res in units) { Console.WriteLine(res); } } }
Output
898 789
Advertisements