LINQ Max Function to Get Maximum Value From List

  Total Views : 421
  Comments : 0
 
  Keywords : How to use linq max() function to find maximum / highest value from the list with example, Linq select / get max value from the list using max() function example, Linq find item with max val

Here we will learn linq max() function / operator in c#, vb.net with example and how to use linq max() function / operator to get maximum value from the list / collection in c#, vb.net with example.

LINQ Max() Function

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.

Syntax of LINQ Max() Function

Following is the syntax of using LINQ Max() function to get maximum value from list or collection.

 

C# Code

 

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int MaximumNum = Num.Max();

 

VB.NET Code

 

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.

LINQ Max() Function Example

Now we will see the example of using LINQ Max() function to get maximum number from the list of array values in applications.

 

C# Code

 

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

}

}

}

VB.NET Code

 

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

In this example we have a integer array “Num” and we will find maximum value from the given array using LINQ Max() function. Now we will run and see the output that would be like as shown below

Output of LINQ Max() Function

Following is the result of using LINQ Max() function to find highest / maximum value from the list.

 

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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User