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

เมธอด Min ของ C# Queryable

ดึงค่าต่ำสุดจากลำดับข้อมูลโดยใช้เมธอด Min() ใน C#

นี่คือลิสต์ของเรา:

List<long> list = new List<long> { 1, 2, 3, 4, 5, 6 };

ตอนนี้ ให้ใช้เมธอด Queryable.Min() เพื่อดึงองค์ประกอบที่น้อยที่สุด:

list.AsQueryable().Min();

ตัวอย่าง

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

class Demo
{
    static void Main()
    {
        List<long> list = new List<long> { 1, 2, 3, 4, 5, 6 };
        
        foreach(long ele in list)
        {
            Console.WriteLine(ele);
        }
        
        // ได้ค่าที่น้อยที่สุด
        long min_num = list.AsQueryable().Min();
        Console.WriteLine("Smallest number = {0}", min_num);
    }
}

ผลลัพธ์

1
2
3
4
5
6
Smallest number = 1