Queryable.Any Metod

Definition

Avgör om något element i en IQueryable<T> sekvens finns eller uppfyller ett villkor.

Överlagringar

Name Description
Any<TSource>(IQueryable<TSource>)

Avgör om en sekvens innehåller några element.

Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Avgör om något element i en sekvens uppfyller ett villkor.

Any<TSource>(IQueryable<TSource>)

Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs

Avgör om en sekvens innehåller några element.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static bool Any(System::Linq::IQueryable<TSource> ^ source);
public static bool Any<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 bool Any<TSource>(this System.Linq.IQueryable<TSource> source);
static member Any : System.Linq.IQueryable<'Source> -> bool
[<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 Any : System.Linq.IQueryable<'Source> -> bool
<Extension()>
Public Function Any(Of TSource) (source As IQueryable(Of TSource)) As Boolean

Typparametrar

TSource

Typen av element sourcei .

Parametrar

source
IQueryable<TSource>

En sekvens för att kontrollera att den är tom.

Returer

trueom källsekvensen innehåller några element; annars . false

Attribut

Undantag

source är null.

Exempel

Följande kodexempel visar hur du använder Any<TSource>(IQueryable<TSource>) för att avgöra om en sekvens innehåller några element.

List<int> numbers = new List<int> { 1, 2 };

// Determine if the list contains any elements.
bool hasElements = numbers.AsQueryable().Any();

Console.WriteLine("The list {0} empty.",
    hasElements ? "is not" : "is");

// This code produces the following output:
//
// The list is not empty.
Dim numbers As New List(Of Integer)(New Integer() {1, 2})

' Determine if the list contains any elements.
Dim hasElements As Boolean = numbers.AsQueryable().Any()

MsgBox(String.Format("The list {0} empty.", _
    IIf(hasElements, "is not", "is")))

' This code produces the following output:
'
' The list is not empty.

Det booleska värde som metoden Any<TSource>(IQueryable<TSource>) returnerar används vanligtvis i predikatet för en where-sats (Where-sats i Visual Basic) eller ett direktanrop till metoden Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>). I följande exempel visas den här användningen av Any metoden.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class Person
{
    public string LastName { get; set; }
    public Pet[] Pets { get; set; }
}

public static void AnyEx2()
{
    List<Person> people = new List<Person>
        { new Person { LastName = "Haas",
                       Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
                                          new Pet { Name="Boots", Age=14 },
                                          new Pet { Name="Whiskers", Age=6 }}},
          new Person { LastName = "Fakhouri",
                       Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
          new Person { LastName = "Antebi",
                       Pets = new Pet[] { }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have a non-empty Pet array.
    IEnumerable<string> names = from person in people
                                where person.Pets.AsQueryable().Any()
                                select person.LastName;

    foreach (string name in names)
        Console.WriteLine(name);

    /* This code produces the following output:

       Haas
       Fakhouri
       Philips
    */
}
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Structure Person
    Public LastName As String
    Public Pets() As Pet
End Structure

Sub AnyEx2()
    Dim people As New List(Of Person)(New Person() _
        {New Person With {.LastName = "Haas", _
                          .Pets = New Pet() {New Pet With {.Name = "Barley", .Age = 10}, _
                                             New Pet With {.Name = "Boots", .Age = 14}, _
                                             New Pet With {.Name = "Whiskers", .Age = 6}}}, _
          New Person With {.LastName = "Fakhouri", _
                           .Pets = New Pet() {New Pet With {.Name = "Snowball", .Age = 1}}}, _
          New Person With {.LastName = "Antebi", _
                           .Pets = New Pet() {}}, _
          New Person With {.LastName = "Philips", _
                           .Pets = New Pet() {New Pet With {.Name = "Sweetie", .Age = 2}, _
                                              New Pet With {.Name = "Rover", .Age = 13}}}})

    ' Determine which people have a non-empty Pet array.
    Dim names = From person In people _
                Where person.Pets.AsQueryable().Any() _
                Select person.LastName

    For Each name As String In names
        Console.WriteLine(name)
    Next

    ' This code produces the following output:
    '
    ' Haas
    ' Fakhouri
    ' Philips

End Sub

Kommentarer

Metoden Any<TSource>(IQueryable<TSource>) genererar en MethodCallExpression som representerar att anropa Any<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 Any<TSource>(IQueryable<TSource>) körs beror på implementeringen av source parametertypen. Det förväntade beteendet är att det avgör om source det innehåller några element.

Gäller för

Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs
Källa:
Queryable.cs

Avgör om något element i en sekvens uppfyller ett villkor.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static bool Any(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static bool Any<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
[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 bool Any<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Any : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> bool
[<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 Any : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> bool
<Extension()>
Public Function Any(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Boolean

Typparametrar

TSource

Typen av element sourcei .

Parametrar

source
IQueryable<TSource>

En sekvens vars element ska testas för ett villkor.

predicate
Expression<Func<TSource,Boolean>>

En funktion för att testa varje element för ett villkor.

Returer

trueom några element i källsekvensen klarar testet i det angivna predikatet. annars . false

Attribut

Undantag

source eller predicate är null.

Exempel

Följande kodexempel visar hur du använder Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) för att avgöra om något element i en sekvens uppfyller ett villkor.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Vaccinated { get; set; }
}

public static void AnyEx3()
{
    // Create an array of Pet objects.
    Pet[] pets =
        { new Pet { Name="Barley", Age=8, Vaccinated=true },
          new Pet { Name="Boots", Age=4, Vaccinated=false },
          new Pet { Name="Whiskers", Age=1, Vaccinated=false } };

    // Determine whether any pets over age 1 are also unvaccinated.
    bool unvaccinated =
        pets.AsQueryable().Any(p => p.Age > 1 && !p.Vaccinated);

    Console.WriteLine(
        "There {0} unvaccinated animals over age one.",
        unvaccinated ? "are" : "are not any");
}

// This code produces the following output:
//
//  There are unvaccinated animals over age one.
Structure Pet
    Dim Name As String
    Dim Age As Integer
    Dim Vaccinated As Boolean
End Structure

Shared Sub AnyEx3()
    ' Create an array of Pet objects.
    Dim pets() As Pet = _
        {New Pet With {.Name = "Barley", .Age = 8, .Vaccinated = True}, _
         New Pet With {.Name = "Boots", .Age = 4, .Vaccinated = False}, _
         New Pet With {.Name = "Whiskers", .Age = 1, .Vaccinated = False}}

    ' Determine whether any pets over age 1 are also unvaccinated.
    Dim unvaccinated As Boolean = _
    pets.AsQueryable().Any(Function(p) p.Age > 1 And p.Vaccinated = False)

    MsgBox(String.Format( _
        "There {0} unvaccinated animals over age one.", _
        IIf(unvaccinated, "are", "are not any") _
    ))
End Sub

' This code produces the following output:
'
'  There are unvaccinated animals over age one.

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 Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) genererar en MethodCallExpression som representerar att anropa Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 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 Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) körs beror på implementeringen av source parametertypen. Det förväntade beteendet är att det avgör om något av elementen source i uppfyller villkoret som anges av predicate.

Gäller för