LINQ Max Function to Get Maximum Value From List

In LINQ, Max() function is used to return 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 normal coding we have to write a quite bit of code to get maximum value from list of values.

 

Following is the syntax of using LINQ Max() function to get maximum value from 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 above syntax we are getting maximum value from "Num" list using LINQ Max() function.

 

Following are the examples of using 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 results like as shown below.

Output of LINQ Max() Function

Following is the result of using 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.

Topics Covered