C# Queryable Min Method

The Min() method in C# is used to find the minimum value from a sequence. When used with AsQueryable(), it creates a queryable data source that can be efficiently processed using LINQ expressions.

Syntax

Following is the syntax for the Queryable Min() method −

public static TSource Min<TSource>(this IQueryable<TSource> source);
public static TResult Min<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector);

Parameters

  • source: The IQueryable sequence to find the minimum value in.
  • selector: A function to extract a value from each element (optional).

Return Value

Returns the minimum value in the sequence. The return type matches the element type or the type specified by the selector function.

Using Min() with Numeric Collections

The most common usage is finding the minimum value in a numeric collection −

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

class Demo {
   static void Main() {
      List<long> list = new List<long> { 1, 2, 3, 4, 5, 6 };
      
      Console.WriteLine("Original list:");
      foreach(long ele in list) {
         Console.WriteLine(ele);
      }
      
      // getting lowest element
      long min_num = list.AsQueryable().Min();
      Console.WriteLine("Smallest number = {0}", min_num);
   }
}

The output of the above code is −

Original list:
1
2
3
4
5
6
Smallest number = 1

Using Min() with Selector Function

You can use a selector function to find the minimum value based on a specific property or calculation −

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

class Student {
   public string Name { get; set; }
   public int Age { get; set; }
   public double Score { get; set; }
}

class Program {
   static void Main() {
      List<Student> students = new List<Student> {
         new Student { Name = "Alice", Age = 20, Score = 85.5 },
         new Student { Name = "Bob", Age = 22, Score = 78.2 },
         new Student { Name = "Charlie", Age = 19, Score = 92.1 }
      };

      // Find minimum age
      int minAge = students.AsQueryable().Min(s => s.Age);
      Console.WriteLine("Minimum age: " + minAge);

      // Find minimum score
      double minScore = students.AsQueryable().Min(s => s.Score);
      Console.WriteLine("Minimum score: " + minScore);
   }
}

The output of the above code is −

Minimum age: 19
Minimum score: 78.2

Using Min() with String Collections

The Min() method can also work with string collections, using lexicographic ordering −

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

class Program {
   static void Main() {
      List<string> names = new List<string> { "John", "Alice", "Bob", "Charlie" };
      
      string minName = names.AsQueryable().Min();
      Console.WriteLine("First name alphabetically: " + minName);
      
      // Find shortest name by length
      string shortestName = names.AsQueryable().Min(n => n).OrderBy(n => n.Length).First();
      Console.WriteLine("Shortest name: " + shortestName);
   }
}

The output of the above code is −

First name alphabetically: Alice
Shortest name: Bob

Comparison: Queryable vs Enumerable Min()

Queryable.Min() Enumerable.Min()
Works with IQueryable<T> Works with IEnumerable<T>
Uses Expression<Func<T, TResult>> Uses Func<T, TResult>
Optimized for database queries Executes in memory
Deferred execution Immediate execution

Conclusion

The Queryable Min() method provides an efficient way to find the minimum value in a sequence. It supports both direct value comparison and custom selector functions, making it versatile for various data types and complex objects.

Updated on: 2026-03-17T07:04:35+05:30

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements