Queryable.Max(Of TSource, TResult) Method (IQueryable(Of TSource), Expression(Of Func(Of TSource, TResult)))
Invokes a projection function on each element of a generic IQueryable(Of T) and returns the maximum resulting value.
Assembly: System.Core (in System.Core.dll)
<ExtensionAttribute> Public Shared Function Max(Of TSource, TResult) ( source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, TResult)) ) As TResult
Parameters
- source
-
Type:
System.Linq.IQueryable(Of TSource)
A sequence of values to determine the maximum of.
- selector
-
Type:
System.Linq.Expressions.Expression(Of Func(Of TSource, TResult))
A projection function to apply to each element.
Return Value
Type: TResultThe maximum value in the sequence.
Type Parameters
- TSource
The type of the elements of source.
- TResult
The type of the value returned by the function represented by selector.
| Exception | Condition |
|---|---|
| ArgumentNullException | source or selector is null. |
This method has at least one parameter of type Expression(Of TDelegate) whose type argument is one of the Func(Of T, TResult) types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression(Of TDelegate).
The Max(Of TSource, TResult)(IQueryable(Of TSource), Expression(Of Func(Of TSource, TResult))) method generates a MethodCallExpression that represents calling Max(Of TSource, TResult)(IQueryable(Of TSource), Expression(Of Func(Of TSource, TResult))) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute(Of 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(Of TSource, TResult)(IQueryable(Of TSource), Expression(Of Func(Of 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.
The following code example demonstrates how to use Max(Of TSource, TResult)(IQueryable(Of TSource), Expression(Of Func(Of TSource, TResult))) to determine the maximum value in a sequence of projected values.
Structure Pet Public Name As String Public Age As Integer End Structure Shared Sub MaxEx2() Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _ New Pet With {.Name = "Boots", .Age = 4}, _ New Pet With {.Name = "Whiskers", .Age = 1}} ' Add Pet.Age to the length of Pet.Name ' to determine the "maximum" Pet object in the array. Dim max As Integer = _ pets.AsQueryable().Max(Function(pet) pet.Age + pet.Name.Length) MsgBox(String.Format("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.
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1