C# LINQ Queryable.Min Method in ASP.Net

Updated on 13 Apr 2012,
Published on 07 Feb 2011

The Min method of System.Linq.Queryable class returns the smallest value from a sequence. It accepts only numeric type IQueryable<T> sequence and returns the minimum value from the specified sequence. The overloaded functions of LINQ Min method also enable to find the minimum value from a sequence containing the Nullable type of numeric values. There are 22 types of overloaded function forms of Min method which allow to get the smallest numeric value of underlying types such as int, decimal, double, float and their Nullable types such as int?, double? Etc. Following are some of the overloaded functions of Min method:

1.

public static int? Min(this IQueryable<int?> source);

It accepts no parameter and returns minimum value from a Nullable int type values of a sequence.

2.

public static int Min(this IQueryable<int> source);

It accepts no parameter and returns minimum value from an int type values of a sequence.

3.

public static double? Min<TSource>(this IQueryable<TSource> source, Func<TSource, double?> selector);

It accepts one parameter as a selector that invokes a transformation function over each nullable double type values of the sequence and returns a minimum value.

4.

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

It accepts one parameter as a selector that invokes a transformation function over each double type values of the sequence and returns a minimum value.

C# LINQ Min Method Example in ASP.Net

// Example 1:
List<int> numbers = new List<int>() { 10, 1, 15, 22, 5, 34, 23, 19 };

double min = numbers.AsQueryable().Min();

Response.Output.Write("Minimum Number: {0}<br />", min);

// Example 2:
List<int?> scores = new List<int?>() { null, 1, 15, 22, 5, 34, 23, 19 };

double? min_scores = scores.AsQueryable().Min();

Response.Output.Write("Minimum Scores: {0}<br />", min_scores);

// Example 3:
string[] names = { "Rex", "Rocky", "Remo", "Robby", "Rover" };

decimal short_name = names.AsQueryable().Min(name => name.Length);

Response.Output.Write("Shortest String Length: {0}<br />", short_name);

// Output:
// Minimum Number: 1
// Minimum Scores: 1
// Shortest String Length: 3

In the above C# sample code we have created three examples to illustrate the functionality of LINQ Min method. In the first example we have used a simple sequence of int type values and retrieved a minimum value from it using the Min method. In the second example we have created a sequence having the Nullable int type values and retrieved a minimum value from it also. But in the last example we have created a string type sequence of items and for getting the minimum value we have used an overloaded function of Min method by specifying a selector transformation for each element of the underlying sequence to get the minimum length of the string values stored in the sequence.

Continue to next tutorial: C# LINQ Queryable.OfType Method in ASP.Net to learn how to extract the specific type elements from a sequence.

0 Responses to "C# LINQ Queryable.Min Method in ASP.Net"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers