Udostępnij za pośrednictwem


Queryable.Max Metoda

Definicja

Przeciążenia

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

Wywołuje funkcję projekcji dla każdego elementu ogólnego IQueryable<T> i zwraca maksymalną wynikową wartość.

Max<TSource>(IQueryable<TSource>)

Zwraca wartość maksymalną w generycznej IQueryable<T>wartości .

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

Zwraca wartość maksymalną w generycznej IQueryable<T>wartości .

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Wywołuje funkcję projekcji dla każdego elementu ogólnego IQueryable<T> i zwraca maksymalną wynikową wartość.

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static TResult Max(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TResult> ^> ^ selector);
public static TResult Max<TSource,TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
public static TResult? Max<TSource,TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TResult? Max<TSource,TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
static member Max : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Result>> -> 'Result
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Max : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Result>> -> 'Result
<Extension()>
Public Function Max(Of TSource, TResult) (source As IQueryable(Of TSource), selector As Expression(Of Func(Of TSource, TResult))) As TResult

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ wartości zwracanej przez funkcję reprezentowaną przez selectorelement .

Parametry

source
IQueryable<TSource>

Sekwencja wartości określających maksymalną wartość.

selector
Expression<Func<TSource,TResult>>

Funkcja projekcji, która ma być stosowana do każdego elementu.

Zwraca

TResult

Maksymalna wartość w sekwencji.

Atrybuty

Wyjątki

source lub selector ma wartość null.

source nie zawiera żadnych elementów.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) metody w celu określenia maksymalnej wartości w sekwencji przewidywanych wartości.

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.
*/
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.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do Execute<TResult>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że wywołuje selector on każdy element w source i zwraca maksymalną wartość.

Dotyczy

Max<TSource>(IQueryable<TSource>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca wartość maksymalną w generycznej IQueryable<T>wartości .

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Max(System::Linq::IQueryable<TSource> ^ source);
public static TSource Max<TSource>(this System.Linq.IQueryable<TSource> source);
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source);
static member Max : System.Linq.IQueryable<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Max : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IQueryable(Of TSource)) As TSource

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Sekwencja wartości określających maksymalną wartość.

Zwraca

TSource

Maksymalna wartość w sekwencji.

Atrybuty

Wyjątki

Parametr source ma wartość null.

source nie zawiera żadnych elementów.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Max<TSource>(IQueryable<TSource>) metody określania maksymalnej wartości w sekwencji.

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.
*/
Dim longs As New List(Of Long)(New Long() {4294967296L, 466855135L, 81125L})

Dim max As Long = longs.AsQueryable().Max()

MsgBox(String.Format("The largest number is {0}.", max))

'This code produces the following output:

'The largest number is 4294967296.

Uwagi

Metoda Max<TSource>(IQueryable<TSource>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Max<TSource>(IQueryable<TSource>) jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do Execute<TResult>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie Max<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca maksymalną wartość w elem source.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca wartość maksymalną w generycznej IQueryable<T>wartości .

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Max(System::Linq::IQueryable<TSource> ^ source, System::Collections::Generic::IComparer<TSource> ^ comparer);
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource? Max<TSource>(this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);
static member Max : System.Linq.IQueryable<'Source> * System.Collections.Generic.IComparer<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Max : System.Linq.IQueryable<'Source> * System.Collections.Generic.IComparer<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IQueryable(Of TSource), comparer As IComparer(Of TSource)) As TSource

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Sekwencja wartości określających maksymalną wartość.

comparer
IComparer<TSource>

Element IComparer<T> do porównywania wartości.

Zwraca

TSource

Maksymalna wartość w sekwencji.

Atrybuty

Wyjątki

Parametr source ma wartość null.

Dotyczy