Enumerable.Any Metoda

Definicja

Określa, czy istnieje dowolny element sekwencji, czy spełnia warunek.

Przeciążenia

Any<TSource>(IEnumerable<TSource>)

Określa, czy sekwencja zawiera jakiekolwiek elementy.

Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Określa, czy dowolny element sekwencji spełnia warunek.

Any<TSource>(IEnumerable<TSource>)

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

Określa, czy sekwencja zawiera jakiekolwiek elementy.

C#
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Aby IEnumerable<T> sprawdzić pustkę.

Zwraca

true jeśli sekwencja źródłowa zawiera jakiekolwiek elementy; w przeciwnym razie , false.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Any metody w celu określenia, czy sekwencja zawiera jakiekolwiek elementy.

C#
List<int> numbers = new List<int> { 1, 2 };
bool hasElements = numbers.Any();

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

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

Wartość logiczna zwracana Any<TSource>(IEnumerable<TSource>) przez metodę jest zwykle używana w predykacie where klauzuli (Where klauzuli w Visual Basic) lub bezpośrednie wywołanie Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metody. W poniższym przykładzie pokazano użycie Any metody .

C#
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.Any()
                                select person.LastName;

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

    /* This code produces the following output:

       Haas
       Fakhouri
       Philips
    */
}

Uwagi

Uwaga

Ta metoda nie zwraca żadnego elementu kolekcji. Zamiast tego określa, czy kolekcja zawiera jakiekolwiek elementy.

Wyliczanie source jest zatrzymywane zaraz po określeniu wyniku.

W składni wyrażenia zapytania języka Visual Basic klauzula Aggregate Into Any() tłumaczy się na wywołanie Anyelementu .

Zobacz też

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

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

Określa, czy dowolny element sekwencji spełnia warunek.

C#
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element IEnumerable<T> , do którego należy zastosować predykat.

predicate
Func<TSource,Boolean>

Funkcja testowania każdego elementu na stanie.

Zwraca

true jeśli sekwencja źródłowa nie jest pusta, a co najmniej jeden z jego elementów przechodzi test w określonym predykacie; w przeciwnym razie , false.

Wyjątki

source lub predicate to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć Any metody w celu określenia, czy dowolny element w sekwencji spełnia warunek.

C#
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 Pets.
    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.Any(p => p.Age > 1 && p.Vaccinated == false);

    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.

Uwagi

Uwaga

Ta metoda nie zwraca żadnego elementu kolekcji. Zamiast tego określa, czy jakiekolwiek elementy kolekcji spełniają warunek.

Wyliczanie source jest zatrzymywane zaraz po określeniu wyniku.

W składni wyrażenia zapytania języka Visual Basic klauzula Aggregate Into Any() tłumaczy się na wywołanie Anyelementu .

Zobacz też

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0