Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Bỏ qua phần tử mảng với Skip() trong C# LINQ

Skip() bỏ qua N phần tử đầu, trả về phần còn lại. Thường dùng cho phân trang.

Ví dụ minh họa

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        int[] points = { 210, 250, 300, 350, 420 };
        Console.WriteLine("Mảng ban đầu...");
        foreach (int res in points)
            Console.WriteLine(res);

        IEnumerable<int> skipEle = points.AsQueryable()
            .OrderByDescending(s => s)
            .Skip(3);

        Console.WriteLine("Đã bỏ qua 3 phần tử...");
        foreach (int res in skipEle)
            Console.WriteLine(res);
    }
}

Kết quả đầu ra

Mảng ban đầu...
210
250
300
350
420
Đã bỏ qua 3 phần tử...
250
210

Giải thích: Sắp xếp giảm: 420, 350, 300, 250, 210. Bỏ 3 đầu (420,350,300), còn 250, 210.