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

 Live Demo

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

Updated on: 23-Jun-2020

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements