LINQ Max Function to Get Maximum Value From List

In LINQ, the Max() function is useful to return the maximum value from the collection. LINQ has made it easy to find the maximum value from a given data source using Max() function. Otherwise, in our standard coding, we have to write quite a bit of code to get maximum value from the list of values.

 

Following is the syntax of using the LINQ Max() function to get the maximum value from the list or collection in c# and vb.net.

LINQ Max() Function Syntax C#

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int MaximumNum = Num.Max();

LINQ Max() Function Syntax VB.NET

Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim minimumNum As Integer = Num.Max()

If you observe the above syntax, we get the maximum value from the "Num" list using the LINQ Max() function.

 

The following are the examples of using the LINQ Max() function to get the maximum number from the list of array values in c# and vb.net.

LINQ Max() Function Example in C#

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

namespace Linqtutorials
{
   class Program
   {
     static void Main(string[] args)
     {
       int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       Console.WriteLine("The Maximum value in the given array is:");
       int maximumNum = Num.Max();
       Console.WriteLine("The maximum Number is {0}", maximumNum);
       Console.ReadLine();
     }
   }
}

LINQ Max() Function Example in VB.NET 

Module Module1
Sub Main()
Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Console.WriteLine("The Maximum value in the given array is:")
Dim maximumNum As Integer = Num.Max()
Console.WriteLine("The maximum Number is {0}", maximumNum)
Console.ReadLine()
End Sub
End Module

if you observe the above examples, we have a integer array “Num” and we are trying to find the maximum value from a given array using LINQ Max() function.

 

When we execute the above examples, we will get the following result.

Output of LINQ Max() Function

Following is the result of using the LINQ Max() function to find the highest / maximum value from the list in c# and vb.net.

 

The Maximum value in the given array is :

The maximum Number is 9

This is how we can use LINQ Max() function to get max / maximum value from the list or collection.