Queryable.All<TSource> 方法

定義

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

C#
public static bool All<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IQueryable<TSource>

要測試其項目是否符合條件的序列。

predicate
Expression<Func<TSource,Boolean>>

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

傳回

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

例外狀況

sourcepredicatenull

範例

下列程式代碼範例示範如何使用 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 來判斷序列中的所有專案是否符合條件。

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

public static void AllEx1()
{
    // 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.AsQueryable().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<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)通常用於 Visual Basic 中子句的述詞 (子句) Where 或直接呼叫 Wherewhere 方法。 下列範例示範這個 方法的使用 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.AsQueryable().All(pet => pet.Age > 5)
                                select person.LastName;

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

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

備註

這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 會產生 , MethodCallExpression 表示呼叫 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionExecute<TResult>(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource

執行表示呼叫 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實 source 作。 預期的行為是它會判斷 中 source 是否符合 條件 predicate的所有專案。

適用於

產品 版本
.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 2.0, 2.1
UWP 10.0