Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด SkipWhile() ของ C# Queryable

ข้ามองค์ประกอบในอาร์เรย์และคืนค่าองค์ประกอบที่เหลือโดยใช้เมธอด SkipWhile()

นี่คืออาร์เรย์ของเรา:

int[] marks = { 45, 88, 55, 90, 95, 85 };

ตอนนี้ ให้ข้ามองค์ประกอบที่มากกว่าหรือเท่ากับ 60 เงื่อนไขที่เรากำหนดโดยใช้ Lambda Expression

IEnumerable<int> selMarks = marks.AsQueryable()
    .OrderByDescending(s => s)
    .SkipWhile(s => s >= 60);

ตัวอย่าง

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

public class Demo
{
    public static void Main()
    {
        int[] marks = { 45, 88, 55, 90, 95, 85 };
        // ข้ามคะแนนที่มากกว่าหรือเท่ากับ 60
        IEnumerable<int> selMarks = marks.AsQueryable()
            .OrderByDescending(s => s)
            .SkipWhile(s => s >= 60);
        
        // แสดงองค์ประกอบที่เหลือ
        Console.WriteLine("Skipped marks > 60...");
        foreach (int res in selMarks)
        {
            Console.WriteLine(res);
        }
    }
}

ผลลัพธ์

Skipped marks > 60...
55
45