Queryable.Max Method

Definition

Overloads

Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)

Invokes a projection function on each element of a generic IQueryable<T> and returns the maximum resulting value.

Max<TSource>(IQueryable<TSource>)

Returns the maximum value in a generic IQueryable<T>.

Max<TSource>(IQueryable<TSource>, IComparer<TSource>)

Returns the maximum value in a generic IQueryable<T>.

Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Invokes a projection function on each element of a generic IQueryable<T> and returns the maximum resulting value.

C#
public static TResult Max<TSource,TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
C#
public static TResult? Max<TSource,TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);

Type Parameters

TSource

The type of the elements of source.

TResult

The type of the value returned by the function represented by selector.

Parameters

source
IQueryable<TSource>

A sequence of values to determine the maximum of.

selector
Expression<Func<TSource,TResult>>

A projection function to apply to each element.

Returns

TResult

The maximum value in the sequence.

Exceptions

source or selector is null.

source contains no elements.

Examples

The following code example demonstrates how to use Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) to determine the maximum value in a sequence of projected values.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void MaxEx2()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    // Add Pet.Age to the length of Pet.Name
    // to determine the "maximum" Pet object in the array.
    int max =
        pets.AsQueryable().Max(pet => pet.Age + pet.Name.Length);

    Console.WriteLine(
        "The maximum pet age plus name length is {0}.",
        max);
}

/*
    This code produces the following output:

    The maximum pet age plus name length is 14.
*/

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

The Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) method generates a MethodCallExpression that represents calling Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) depends on the implementation of the type of the source parameter. The expected behavior is that it invokes selector on each element in source and returns the maximum value.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Max<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Returns the maximum value in a generic IQueryable<T>.

C#
public static TSource Max<TSource>(this System.Linq.IQueryable<TSource> source);
C#
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

A sequence of values to determine the maximum of.

Returns

TSource

The maximum value in the sequence.

Exceptions

source is null.

source contains no elements.

Examples

The following code example demonstrates how to use Max<TSource>(IQueryable<TSource>) to determine the maximum value in a sequence.

C#
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };

long max = longs.AsQueryable().Max();

Console.WriteLine("The largest number is {0}.", max);

/*
    This code produces the following output:

    The largest number is 4294967296.
*/

Remarks

The Max<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Max<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling Max<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is that it returns the maximum value in source.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Max<TSource>(IQueryable<TSource>, IComparer<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Returns the maximum value in a generic IQueryable<T>.

C#
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

A sequence of values to determine the maximum value of.

comparer
IComparer<TSource>

The IComparer<T> to compare values.

Returns

TSource

The maximum value in the sequence.

Exceptions

source is null.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9