个性化阅读
专注于IT技术分析

LINQ Max()函数

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();

遵循上述语法后, 我们使用LINQ Max()函数从” a”列表中获取最大值。

示例:在此示例中, 我们使用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();
        }
    }
}

输出

从上面的示例中, 它将显示整数数组” a”, 并且我们正在使用LINQ Max()函数从给定的数组中找到最大值。

现在, 我们运行该应用程序, 它将显示列表中的最大值, 如下面控制台窗口中的输出所示。

LINQ Max()函数

赞(0)
未经允许不得转载:srcmini » LINQ Max()函数

评论 抢沙发

评论前必须登录!