LINQ Min Function to Get Minimum Value From List

In LINQ, the Min() function is useful for getting the minimum value from a collection or list. LINQ has made it very easy to find the minimum value from a given data source. Otherwise, in our standard coding, we have to write quite a bit of code to get the minimum value from the list of available values.

 

Following is the syntax of using linq min() function to find the minimum value from the list of values.

LINQ MIN() Function Syntax in C#

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

LINQ MIN() Function Syntax in VB.NET

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

If you observe the above syntax, we are trying to get the minimum value from the "Num" list using the linq Min() function.

 

We will see the complete examples of using the LINQ Min() function to get the minimum value from the list in c# and vb.net applications.

LINQ Min() 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 Minimum value in the given array is:");
      int minimumNum = Num.Min();
      Console.WriteLine("The minimum Number is {0}", minimumNum);
      Console.ReadLine();
    }
  }
}

LINQ Min() 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 Minimum value in the given array is:")
Dim minimumNum As Integer = Num.Min()
Console.WriteLine("The minimum Number is {0}", minimumNum)
Console.ReadLine()
End Sub
End Module

If you observe the above examples, we have a integer array “Num” and we are finding the minimum value from the given array using LINQ Min() function. 

 

When we execute the above examples, we will get the result as shown below.

Output of LINQ Min() Function

Following is the result of using the LINQ Min() function to find the lowest / minimum value from the list.

 

The minimum value in the given array is:

The Minimum Number is 1

This is how we can use linq Min() funciton in c# and vb.net to get the minimum value from the list/collection.