C# Program to return specified number of elements from the beginning of a sequence

CsharpProgrammingServer Side Programming

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
raja
Published on 14-Sep-2018 18:21:53
Advertisements