Enumerable.All<TSource> Yöntem

Tanım

Bir dizinin tüm öğelerinin bir koşulu karşılayıp karşılamadığını belirler.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

IEnumerable<T> Koşulun uygulanacağı öğeleri içeren.

predicate
Func<TSource,Boolean>

Bir koşul için her öğeyi test etmek için bir işlev.

Döndürülenler

true kaynak dizisinin her öğesi belirtilen koşulda testi geçerse veya dizi boşsa; aksi takdirde , false.

Özel durumlar

source veya predicate şeklindedir null.

Örnekler

Aşağıdaki kod örneği, bir dizideki tüm öğelerin bir koşulu karşılayıp karşılamadığını belirlemek için nasıl kullanılacağını All gösterir. Tüm evcil hayvan adları "B" ile başlıyorsa veya dizi boşsa pets değişken allStartWithB doğrudur.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void AllEx()
{
    // Create an array of Pets.
    Pet[] pets = { new Pet { Name="Barley", Age=10 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=6 } };

    // Determine whether all pet names
    // in the array start with 'B'.
    bool allStartWithB = pets.All(pet =>
                                      pet.Name.StartsWith("B"));

    Console.WriteLine(
        "{0} pet names start with 'B'.",
        allStartWithB ? "All" : "Not all");
}

// This code produces the following output:
//
//  Not all pet names start with 'B'.

Yöntemin All döndürdüğü Boole değeri genellikle bir where yan tümcesinin (Where Visual Basic'teki yan tümcesi) veya yöntemine doğrudan çağrı koşulunda Where kullanılır. Aşağıdaki örnekte yönteminin bu kullanımı gösterilmektedir All .

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 AllEx2()
{
    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 Pet { Name = "Belle", Age = 8} }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have pets that are all older than 5.
    IEnumerable<string> names = from person in people
                                where person.Pets.All(pet => pet.Age > 5)
                                select person.LastName;

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

    /* This code produces the following output:
     *
     * Haas
     * Antebi
     */
}

Açıklamalar

Not

Bu yöntem bir koleksiyonun tüm öğelerini döndürmez. Bunun yerine, bir koleksiyonun tüm öğelerinin bir koşulu karşılayıp karşılamadığını belirler.

Numaralandırması source , sonuç belirlenebildiği anda durdurulur.

Visual Basic sorgu ifadesi söz diziminde yan Aggregate Into All() tümcesi çağrısına çevrilir All.

Şunlara uygulanır

Ürün Sürümler
.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

Ayrıca bkz.