Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C# 시퀀스 시작 부분에서 지정된 수의 요소를 반환하는 프로그램

<시간/>

배열을 설정하고 OrderByDescending을 사용하여 내림차순으로 정렬합니다.

int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};

이제 Take() 메서드를 사용하여 처음부터 지정된 수의 요소를 반환합니다.

Enumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2);

전체 코드를 살펴보겠습니다.

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);
      }
   }
}

출력

898
789