Queryable.Max Metode

Definisi

Overload

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

Memanggil fungsi proyeksi pada setiap elemen generik IQueryable<T> dan mengembalikan nilai maksimum yang dihasilkan.

Max<TSource>(IQueryable<TSource>)

Mengembalikan nilai maksimum dalam generik IQueryable<T>.

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

Mengembalikan nilai maksimum dalam generik IQueryable<T>.

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

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Memanggil fungsi proyeksi pada setiap elemen generik IQueryable<T> dan mengembalikan nilai maksimum yang dihasilkan.

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);
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

Jenis parameter

TSource

Jenis elemen source.

TResult

Jenis nilai yang dikembalikan oleh fungsi yang diwakili oleh selector.

Parameter

source
IQueryable<TSource>

Urutan nilai untuk menentukan maksimum.

selector
Expression<Func<TSource,TResult>>

Fungsi proyeksi untuk diterapkan ke setiap elemen.

Mengembalikan

TResult

Nilai maksimum dalam urutan.

Pengecualian

source atau selector adalah null.

source tidak berisi elemen.

Contoh

Contoh kode berikut menunjukkan cara menggunakan Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) untuk menentukan nilai maksimum dalam urutan nilai yang diproyeksikan.

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.

Keterangan

Metode ini memiliki setidaknya satu parameter jenis Expression<TDelegate> yang argumen jenisnya adalah salah satu jenisnya Func<T,TResult> . Untuk parameter ini, Anda dapat meneruskan ekspresi lambda dan akan dikompilasi ke Expression<TDelegate>.

Metode ini Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) menghasilkan MethodCallExpression yang mewakili pemanggilan Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke Execute<TResult>(Expression) metode dari yang IQueryProvider diwakili oleh Provider properti source parameter .

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah bahwa ia memanggil selector pada setiap elemen di source dan mengembalikan nilai maksimum.

Berlaku untuk

Max<TSource>(IQueryable<TSource>)

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan nilai maksimum dalam generik IQueryable<T>.

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);
static member Max : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IQueryable(Of TSource)) As TSource

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Urutan nilai untuk menentukan maksimum.

Mengembalikan

TSource

Nilai maksimum dalam urutan.

Pengecualian

sourceadalah null.

source tidak berisi elemen.

Contoh

Contoh kode berikut menunjukkan cara menggunakan Max<TSource>(IQueryable<TSource>) untuk menentukan nilai maksimum secara berurutan.

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.

Keterangan

Metode ini Max<TSource>(IQueryable<TSource>) menghasilkan MethodCallExpression yang mewakili pemanggilan Max<TSource>(IQueryable<TSource>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke Execute<TResult>(Expression) metode dari yang IQueryProvider diwakili oleh Provider properti source parameter .

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan Max<TSource>(IQueryable<TSource>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah mengembalikan nilai maksimum dalam source.

Berlaku untuk

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

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan nilai maksimum dalam generik IQueryable<T>.

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);
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

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Urutan nilai untuk menentukan nilai maksimum.

comparer
IComparer<TSource>

IComparer<T> untuk membandingkan nilai.

Mengembalikan

TSource

Nilai maksimum dalam urutan.

Pengecualian

sourceadalah null.

Berlaku untuk