LINQ的min()函数可用于从集合或列表中获取最小值。LINQ使得从给定的数据源查找最小值变得非常容易。在编码时,我们必须编写一个位代码,以便从可用值的列表中获取最小值。
LINQ Min () Function Syntax in C#
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int minimumNum = a.Min();
From the above syntax, we are getting the minimum value from the "a" list using the LINQ Min () function.
现在,我们将看到在C#应用程序中使用LINQMin()函数从列表中获取最小值的完整示例。
using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Create an array a type of int having the values 1 to 9
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("The Minimum value in the given array is:");
/Min() function is applied on the array a
to find the minimum number from the array/
int minimumNum = a.Min();
Console.WriteLine("The minimum Number is {0}", minimumNum);
Console.ReadLine();
}
}
}
From the above example, it will show the integer array "a" and we are finding the minimum value from the given array using LINQ() function.
Now we run the application, and it will display the minimum value from the list, as shown below in the output in the console window.
输出
