Queryable.Max Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Överlagringar
| Name | Description |
|---|---|
| Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) |
Anropar en projektionsfunktion på varje element i en allmän IQueryable<T> och returnerar det maximala resulterande värdet. |
| Max<TSource>(IQueryable<TSource>) |
Returnerar det maximala värdet i en allmän IQueryable<T>. |
| Max<TSource>(IQueryable<TSource>, IComparer<TSource>) |
Returnerar det maximala värdet i en allmän IQueryable<T>. |
Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
Anropar en projektionsfunktion på varje element i en allmän IQueryable<T> och returnerar det maximala resulterande värdet.
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
Typparametrar
- TSource
Typen av element sourcei .
- TResult
Typen av värde som returneras av funktionen som representeras av selector.
Parametrar
- source
- IQueryable<TSource>
En sekvens med värden för att fastställa maxvärdet för.
- selector
- Expression<Func<TSource,TResult>>
En projektionsfunktion som ska tillämpas på varje element.
Returer
Maximalt värde i sekvensen.
- Attribut
Undantag
source eller selector är null.
source innehåller inga element.
Exempel
Följande kodexempel visar hur du använder Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) för att fastställa det maximala värdet i en sekvens med beräknade värden.
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.
Kommentarer
Den här metoden har minst en parameter av typen Expression<TDelegate> vars typargument är en av typerna Func<T,TResult> . För dessa parametrar kan du skicka ett lambda-uttryck och kompileras till en Expression<TDelegate>.
Metoden Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) genererar en MethodCallExpression som representerar att anropa Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) sig själv som en konstruerad generisk metod. Den skickar sedan till MethodCallExpression metoden för den Execute<TResult>(Expression) som representeras av IQueryProvider egenskapen för parameternProvider.source
Frågebeteendet som uppstår till följd av att ett uttrycksträd som representerar anrop Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) körs beror på implementeringen av source parametertypen. Det förväntade beteendet är att det anropar selector på varje element i source och returnerar det maximala värdet.
Gäller för
Max<TSource>(IQueryable<TSource>)
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
Returnerar det maximala värdet i en allmän 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);
[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
Typparametrar
- TSource
Typen av element sourcei .
Parametrar
- source
- IQueryable<TSource>
En sekvens med värden för att fastställa maxvärdet för.
Returer
Maximalt värde i sekvensen.
- Attribut
Undantag
source är null.
source innehåller inga element.
Exempel
Följande kodexempel visar hur du använder Max<TSource>(IQueryable<TSource>) för att fastställa det maximala värdet i en sekvens.
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.
Kommentarer
Metoden Max<TSource>(IQueryable<TSource>) genererar en MethodCallExpression som representerar att anropa Max<TSource>(IQueryable<TSource>) sig själv som en konstruerad generisk metod. Den skickar sedan till MethodCallExpression metoden för den Execute<TResult>(Expression) som representeras av IQueryProvider egenskapen för parameternProvider.source
Frågebeteendet som uppstår till följd av att ett uttrycksträd som representerar anrop Max<TSource>(IQueryable<TSource>) körs beror på implementeringen av source parametertypen. Det förväntade beteendet är att det returnerar det maximala värdet i source.
Gäller för
Max<TSource>(IQueryable<TSource>, IComparer<TSource>)
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
- Källa:
- Queryable.cs
Returnerar det maximala värdet i en allmän 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);
[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
Typparametrar
- TSource
Typen av element sourcei .
Parametrar
- source
- IQueryable<TSource>
En sekvens med värden för att fastställa det maximala värdet för.
- comparer
- IComparer<TSource>
Att IComparer<T> jämföra värden.
Returer
Maximalt värde i sekvensen.
- Attribut
Undantag
source är null.