Summary: in this tutorial, you’ll learn how to use the LINQ Min()
method to get the minimum element in a sequence.
Introduction to the LINQ Min() method
The
is a LINQ extension method that returns the minimum value in a sequence of values. The syntax of the Min()
method is as follows:Min()
public static TResult? Min (
this IEnumerable<TSource?> source
);
Code language: C# (cs)
In this syntax:
TSource
is the type of element in thesource
sequence.source
is the input sequence that thas the IEnumerable<T>.
The Min()
returns the minimum value of the source
sequence. It ignores null
in the source
sequence.
If the source
sequence is empty, the Min()
throws an InvalidOperationException
. Also, if the source
sequence is null
, the method throws an ArgumentNullexception
.
The Min()
method has an overload that accepts a selector function as the second argument:
public static TResult? Min (
this IEnumerable<T?> source
Func<TSource,TResult> selector
);
Code language: C# (cs)
In this syntax, the selector
is a transform function that applies to each element of the source
sequence.
LINQ Min() method examples
Let’s take some examples of using the LINQ Min()
method.
1) Using the LINQ Min() method to find the minimum value in an array of integers
The following program demonstrates how to use the Min()
method to find the minimum number in an array of integers:
int[] numbers = { 5, 2, 8, 3, 7 };
int min = numbers.Min();
Console.WriteLine($"Minimum value = {min} ");
Code language: C# (cs)
Output:
Minimum value = 2
Code language: C# (cs)
2) Using the LINQ Min() method with a selector function example
The following example demonstrates how to use the Min()
method with a selector function to find the minimum length of a sequence of strings:
string[] words = { "apple", "banana", "mango" };
int minLength = words.Min(word => word.Length);
Console.WriteLine($"Minimum length = {minLength}");
Code language: C# (cs)
Output:
Minimum length = 5
Code language: C# (cs)
3) Using the LINQ Min() method to find a string with an empty sequence
If the input sequence is empty, the Min()
method throws an InvalidOperationException
. In this case, you can use the DefaultIfEmpty()
method to specify a default minimum value and avoid the exception.
For example:
int[] lengths = {};
var minLength = lengths.DefaultIfEmpty(0).Min();
Console.WriteLine($"Minimum value = {minLength}");
Code language: C# (cs)
Output:
Minimum value = 0
Code language: C# (cs)
3) Using the LINQ Min() method with a sequence of objects of a custom type
Suppose you have a Product class with two properties Name and Price:
public class Product
{
public string Name
{
get; set;
}
public decimal Price
{
get; set;
}
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}
Code language: JavaScript (javascript)
The following program uses the Min() method to find the minimum price in a list of products:
using static System.Console;
var products = new List<Product>() {
new Product("A",100),
new Product("B",200),
new Product("C",150),
};
var minPrice = products.Min(p => p.Price);
WriteLine(minPrice);
Code language: JavaScript (javascript)
Output:
100
It returns 100 as expected.
To find a product that has a minimum price, you can use the MinBy()
method.
Summary
- Use the LINQ
Min()
method to find the minimum value in a sequence. - Use the
DefaultIfEmpty()
method with theMin()
method to specify a default minimum value if the sequence is empty.