Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Min Operator

Modified: 2007/01/23 01:48 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Min operator finds the minimum of a sequence of numeric values.

Edit

Method Signatures

// 1 - Return the minimum value
public static decimal Min(
    this IEnumerable<decimal> source)
 
public static double Min(
    this IEnumerable<double> source)
 
public static int Min(
    this IEnumerable<int> source)
 
public static TSource Min<TSource>(
    this IEnumerable<TSource> source)
 
public static Nullable<int> Min(
    this IEnumerable<Nullable<int>> source)
 
public static Nullable<double> Min(
    this IEnumerable<Nullable<double>> source)
 
public static long Min(
    this IEnumerable<long> source)
 
public static Nullable<decimal> Min(
    this IEnumerable<Nullable<decimal>> source)
 
public static Nullable<long> Min(
    this IEnumerable<Nullable<long>> source)
 
public static Nullable<float> Min(
    this IEnumerable<Nullable<float>> source)
 
public static float Min(
    this IEnumerable<float> source)
 
// 2 - Return the minimum value using the Select projector.
public static Nullable<int> Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<int>> selector)
 
public static Nullable<long> Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<long>> selector)
 
public static decimal Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, decimal> selector)
 
public static Nullable<float> Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<float>> selector)
 
public static TResult Min<TSource, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, TResult> selector)
 
public static Nullable<decimal> Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<decimal>> selector)
 
public static double Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, double> selector)
 
public static Nullable<double> Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<double>> selector)
 
public static int Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, int> selector)
 
public static long Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, long> selector)
 
public static float Min<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, float> selector)



Edit

Exceptions

Throws an ArgumentNullException if source or selector is null.
Throws an InvalidOperationException if source has no elements and the TSource is not a nullable type.


Edit

Pseudo-code

Overload 1
If source is null, throw an ArgumentNullException.
If source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else.
Throw an InvalidOperationException.
Iterate the source sequence.
If the current element is not null.
If the current value is the first or lowest value yet found, remember it.
Return the lowest value found.


Overload 2
If source is null, throw an ArgumentNullException.
If source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else
Throw an InvalidOperationException.
If selector is null, throw an ArgumentNullException.
Iterate the source sequence.
Get the current value by projecting the current element using the selector function.
If the current element is not null.
If the current value is the first or lowest value yet found, remember it.
Return the lowest value found.


Note: If an element is null, then it is skipped in the count and does not affect the sum.


Edit

Loop Count

1. All elements from the source sequence are enumerated.


Edit

Code Samples

Min Operator Unit Tests

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.