LINQ中的Max()函数用于返回集合中的最大值。在Max()函数的帮助下,使用Max()函数可以很容易地从给定的数据源找到最大值。在另一种情况下,我们必须编写代码以从值列表中获取最大值。
下面是使用LINQ Max()函数从C#中的列表或集合中获取最大值的语法。
C#中的Linq Max()函数语法
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int MaximumNum = a.Max();
After observing the above syntax, we are getting the maximum value from the "a" list using LINQ Max () function.
示例:在此示例中,我们使用Max()函数从C#中的数组值列表中获取最大值。
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 named a type of int having the values 1 t 9
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("The Maximum value in the array is:");
/Max() function is applied on the array to
find the maximum value from the arra/
int maximumNum = a.Max();
Console.WriteLine("The maximum Number is {0}", maximumNum);
Console.ReadLine();
}
}
}
输出
From the above example, it will show the integer array "a," and we are finding the maximum value from the given array using LINQ Max () function.
现在我们运行应用程序,它将显示列表中的最大值,如控制台窗口的输出中所示。
