Take() 메서드를 사용하여 처음부터 지정된 수의 요소를 가져옵니다.
다음은 우리의 배열입니다.
int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 }; 이제 OrderByDescending을 사용하여 요소를 내림차순으로 정렬합니다. 그런 다음 Take() 메서드를 사용하여 요소를 가져옵니다.
marks.AsQueryable().OrderByDescending(s => s).Take(5);
전체 예를 살펴보겠습니다.
예
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);
}
}
} 출력
95 90 85 72 67