Enumerable.All<TSource> 方法

定義

判斷序列的所有項目是否全都符合條件。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

IEnumerable<T>,其中包含要套用述詞 (Predicate) 的項目。

predicate
Func<TSource,Boolean>

用來測試每個項目是否符合條件的函式。

傳回

如果來源序列的每個項目都通過以指定之述詞 (Predicate) 進行的測試,或序列是空的,則為 true,否則為 false

例外狀況

sourcepredicatenull

範例

下列程式代碼範例示範如何使用 All 來判斷序列中的所有元素是否符合條件。 如果所有寵物名稱都是以 「B」 開頭,或陣列是空的,pets則變數allStartWithB為 true。

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'.

方法傳回的All布爾值通常用於 Visual Basic) 或直接呼叫 Where 方法的子句 (Where 子句述where詞中。 下列範例示範此方法的使用 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
     */
}

備註

備註

這個方法不會傳回集合的所有專案。 相反地,它會判斷集合的所有元素是否符合條件。

只要判斷結果,就會停止 列舉 source

在 Visual Basic 查詢表達式語法中 Aggregate Into All() ,子句會轉譯為的 All調用。

適用於

產品 版本
.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

另請參閱